|
-
Sep 29th, 2008, 06:03 PM
#1
Thread Starter
Hyperactive Member
Removing Text from Beginning and From Middle of a String
Heres the String I need to remove from:
Start: 29/09/2008 End: 29/09/2008
Heres the code that I've been messing with, and I just get myself confused, very quickly
vb Code:
Dim DRow As DataRow = DS.Tables(0).Rows(.cmbEditEvents.SelectedIndex) 'Start: 00/00/0000 End: 00/00/0000 Dim SRemoveLen1 As Integer = DRow.Item("Date (D/M/Y)").ToString.IndexOf(":") + 1 Dim ERemoveLen1 As Integer = DRow.Item("Date (D/M/Y)").ToString.LastIndexOf(":") + 1 Dim LenghtofString As Integer = Len(DRow.Item("Date (D/M/Y)")) Dim StartRange As String = DRow.Item("Date (D/M/Y)").ToString.Remove(0, SRemoveLen1) Dim SRemoveLen2 As Integer = StartRange.IndexOf("E") Dim EndRange As String = DRow.Item("Date (D/M/Y)").ToString.Remove(SRemoveLen2, LenghtofString - SRemoveLen2) MsgBox(EndRange)
What I'm trying to do is
set StartRange to 29/09/2008
and EndRange to 29/09/2008
then I can use the .SelectionRange.Start & .SelectionRange.End on the Month Cal to set the Selection Range.
-
Sep 29th, 2008, 06:26 PM
#2
Re: Removing Text from Beginning and From Middle of a String
use regex:
vb Code:
Dim testStr As String = "Start: 29/09/2008 End: 29/09/2008"
Dim rx As New Regex("(?<=Start: )\d{2}\/\d{2}\/\d{4}")
Dim StartRange As String = rx.Match(testStr).Value
rx = New Regex("(?<=End: )\d{2}\/\d{2}\/\d{4}")
Dim EndRange As String = rx.Match(testStr).Value
MsgBox(StartRange & vbCrLf & EndRange)
don't forget to import regex:
vb Code:
Imports System.Text.RegularExpressions
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 29th, 2008, 06:27 PM
#3
Thread Starter
Hyperactive Member
Re: Removing Text from Beginning and From Middle of a String
Umm I rather not use Regex - I'm getting Data from a Access Database, so I rather use Indexs and Removes Please.
-
Sep 29th, 2008, 06:30 PM
#4
Re: Removing Text from Beginning and From Middle of a String
this will work as long as the format is what you showed
Code:
Dim seSTR As String = "Start: 29/09/2008 End: 29/09/2008"
seSTR = seSTR.Replace(" ", " ")
Dim arraySTR() As String = seSTR.Split(" "c)
Dim startRng As String = arraySTR(1)
Dim endRng As String = arraySTR(3)
-
Sep 29th, 2008, 06:31 PM
#5
Re: Removing Text from Beginning and From Middle of a String
ok. try this then:
vb Code:
Dim testStr As String = "Start: 29/09/2008 End: 29/09/2008"
Dim StartRange As String = testStr.Substring(testStr.IndexOf("Start: ") + 7, 10)
Dim EndRange As String = testStr.Substring(testStr.IndexOf("End: ") + 5, 10)
MsgBox(StartRange & vbCrLf & EndRange)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 29th, 2008, 06:32 PM
#6
Re: Removing Text from Beginning and From Middle of a String
ya' just gotta love regex.
-
Sep 29th, 2008, 06:35 PM
#7
Re: Removing Text from Beginning and From Middle of a String
@wesley - does jmc know about your signature?
-
Sep 29th, 2008, 06:36 PM
#8
Thread Starter
Hyperactive Member
Re: Removing Text from Beginning and From Middle of a String
Don't know, but he helps me out a lot, and I mean a lot, I guess I should remove it. Care to explain
Dim testStr As String = "Start: 29/09/2008 End: 29/09/2008"
Dim StartRange As String = testStr.Substring(testStr.IndexOf("Start: ") + 7, 10)
Dim EndRange As String = testStr.Substring(testStr.IndexOf("End: ") + 5, 10)
MsgBox(StartRange & vbCrLf & EndRange)
Is it possible to remove the Random Number (5, 10, etc...) I like using Variables, since it might change from time to time.
-
Sep 29th, 2008, 06:40 PM
#9
Re: Removing Text from Beginning and From Middle of a String
hint - "Start: " is 7 characters
it would help if you explained exactly what the format of the string
Start: 29/09/2008 End: 29/09/2008
is?
-
Sep 29th, 2008, 06:40 PM
#10
Re: Removing Text from Beginning and From Middle of a String
 Originally Posted by Wesley008
Don't know, but he helps me out a lot, and I mean a lot, I guess I should remove it. Care to explain
Dim testStr As String = "Start: 29/09/2008 End: 29/09/2008"
Dim StartRange As String = testStr.Substring(testStr.IndexOf("Start: ") + 7, 10)
Dim EndRange As String = testStr.Substring(testStr.IndexOf("End: ") + 5, 10)
MsgBox(StartRange & vbCrLf & EndRange)
Is it possible to remove the Random Number (5, 10, etc...) I like using Variables, since it might change from time to time.
its not a random number. "End: " is 5 characters, "Start: " is 7 characters + the date in that format is always 10 characters.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 29th, 2008, 11:27 PM
#11
Re: Removing Text from Beginning and From Middle of a String
If the original string is always in the same format, I would remove the IndexOf usage all together:
Code:
Dim originalString As String = "Start: 29/09/2008 End: 29/09/2008"
Dim startString As String = originalString.Substring(7, 10)
Dim endString As String = originalString.Substring(23, 10)
If it is not in the same format everytime, I would use regex.
That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma
Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|