tags:

views:

89

answers:

4

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

+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
Thanks Its Working +1 vote
Rajasekar
A: 

You could loop through each character of the string and check the .IsNumber() on it.

Ken
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
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
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