hi, I am trying to seperate numbers from a string which includes %,/,
etc for eg (%2459348?:
, or :2434545/%
). How can i seperate it, in VB.net
views:
89answers:
4
+3
A:
you want only the numbers right?
then you could do it like this
Dim theString As String = "/79465*44498%464"
Dim ret = Regex.Replace(theString, "[^0-9]", String.Empty)
hth
edit:
or do you want to split by all non number chars? then it would go like this
Dim ret = Regex.Split(theString, "[^0-9]")
marc.d
2009-08-27 15:49:28
Thanks Its Working +1 vote
Rajasekar
2009-08-27 16:01:34
A:
You could loop through each character of the string and check the .IsNumber() on it.
Ken
2009-08-27 15:50:53
A:
This should do:
Dim test As String = "%2459348?:"
Dim match As Match = Regex.Match(test, "\d+")
If match.Success Then
Dim result As String = match.Value
' Do something with result
End If
Result = 2459348
Ahmad Mageed
2009-08-27 16:01:42
Note that this would only match if there's a string of numbers within the text. If there are many numbers with other characters in between them then marc.d's answer is very good (his sample data reflects that scenario).
Ahmad Mageed
2009-08-27 16:27:44
A:
Here's a function which will extract all of the numbers out of a string.
Public Function GetNumbers(ByVal str as String) As String
Dim builder As New StringBuilder()
For Each c in str
If Char.IsNumber(c) Then
builder.Append(c)
End If
Next
return builder.ToString()
End Function
JaredPar
2009-08-27 19:49:43