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.
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
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.
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)
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)
Re: Removing Text from Beginning and From Middle of a String
ya' just gotta love regex.
Re: Removing Text from Beginning and From Middle of a String
@wesley - does jmc know about your signature?
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.
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?
Re: Removing Text from Beginning and From Middle of a String
Quote:
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.
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.