Member_2_1242703
asked on
VB.net substring help
I'm trying to extract text..
If the first 12 characters are 5 numbers followed by a comma followed by 5 numbers followed by a comma, I want to assign everything following the second comma to a vaiable
i.e.
00582,00087,blahblah
myString would = blahblah
TIA
If the first 12 characters are 5 numbers followed by a comma followed by 5 numbers followed by a comma, I want to assign everything following the second comma to a vaiable
i.e.
00582,00087,blahblah
myString would = blahblah
TIA
ASKER
Probably would work for me but what if the string is...
impor,tant,info,41341231
???
impor,tant,info,41341231
???
Assuming that the string format is fixed:
Otherwise you could use code similar to what I answered in the last question about getting the content to the right of a particular character.
Dim myString As String = teststring.Substring(12)
Otherwise you could use code similar to what I answered in the last question about getting the content to the right of a particular character.
ASKER
It would only be fixed if there is the format of 5number comma 5 number comma
Could be
blah,blah,blah
in which case I would not want to change the string
Could be
blah,blah,blah
in which case I would not want to change the string
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try
Dim idx As Integer = str.indexof(",")
if idx = 5 Then
if str.Indexof(",", idx)=11 Then
'further checks here and substring
End If
End If
Dim idx As Integer = str.indexof(",")
if idx = 5 Then
if str.Indexof(",", idx)=11 Then
'further checks here and substring
End If
End If
Regex is probably overkill for this, but you could do:
Dim myString As String
Dim m As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(input, "^\d{5},\d{5},(.*)")
If m.Success Then
myString = m.Groups(1).Value
Else
myString = String.Empty
End If
from:
dim OrigString as string = "00582,00087,blahblah"
dim myString as String = SubString(NthIndexOf(OrigS tring, "," , 2))
dim OrigString as string = "00582,00087,blahblah"
dim myString as String = SubString(NthIndexOf(OrigS
Public NotInheritable Class StringExtender
Private Sub New()
End Sub
<System.Runtime.CompilerServices.Extension> _
Public Shared Function NthIndexOf(target As String, value As String, n As Integer) As Integer
Dim m As Match = Regex.Match(target, "((" & value & ").*?){" & n & "}")
If m.Success Then
Return m.Groups(2).Captures(n - 1).Index
Else
Return -1
End If
End Function
End Class
dim myString as String
myString = OrigString.Split(",").Last