How to get a number out of string

  • Thread starter Thread starter Peter Stojkovic
  • Start date Start date
P

Peter Stojkovic

I have a string in form "abcdefg 123,55 ijklmn"

Now I want get only the number 123,55 within the string !!
Is there any function available in VB.NET ??



Thanks
for any help
 
Hi,

Dim s As String = "abcdefg 123,55 ijklmn"

Dim strWord() As String = s.Split(" ".ToCharArray)

Dim strPart As String
For Each strPart In strWord
Trace.WriteLine(strPart)
Next


Ken
 
Peter said:
I have a string in form "abcdefg 123,55 ijklmn"

Now I want get only the number 123,55 within the string !!
Is there any function available in VB.NET ??

I have once written this code based on a sample I found somewhere on the
web in VB6. Maybe you can convert it to .NET:

\\\
Private Function PurgeNumericInput(ByVal strString As String) As Variant
Dim blnHasDecimal As Boolean, i As Integer
Dim s As String
strString = Trim$(strString)
If Len(strString) = 0 Then
Exit Function
End If
For i = 1 To Len(strString)
Select Case Mid$(strString, i, 1)

' Is this character a number?
Case "0" To "9"

' Add it to the string being built.
s = s & Mid$(strString, i, 1)

' The char is a decimal.
Case ".", ","
If Not blnHasDecimal Then
blnHasDecimal = True
s = s & "."
End If
End Select
Next i
PurgeNumericInput = Val(s)
End Function

Private Sub Main()
Call MsgBox( _
PurgeNumericInput( _
"$44E.mbedded letters and spaces 33 a few more " & _
"pieces of garbage .9" _
) _
)
End Sub
///

It's very quick and dirty, BTW.
 
I have a string in form "abcdefg 123,55 ijklmn"
Now I want get only the number 123,55 within the string !!
Is there any function available in VB.NET ??

If the numbers are always in teh same place you can do a substring argument.

If not you can always test each character for integer conversions (provided
you do not need the comma) and those that fail reject and move on to the
next character.
 
Peter Stojkovic said:
I have a string in form "abcdefg 123,55 ijklmn"

Now I want get only the number 123,55 within the string !!
Is there any function available in VB.NET ??


Is there a general rule for the format of the string? Something like "the
number is always between the 1st and 2nd blank". Have a look at the members
of the String object to find chars and to extract parts of the string.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Hi,

Dim m As System.Text.RegularExpressions.Match
For Each m In System.Text.RegularExpressions.Regex.Matches("abcdefg
123,55 ijklmnabcs123.4", "\d+(\.?\d+)")
Trace.WriteLine(m.Value)
Next

Ken
 
Hi Peter,

Almost the same as Ken's first answer however now with more delimiters.

Dim s As String = "abcdefg 123,55 ijklmn"
Dim delimStr As String = " ,.:;#"
Dim delimiter As Char() = delimStr.ToCharArray()
Dim strWord() As String = s.Split(delimiter)
Dim strPart As String
For Each strPart In strWord
Trace.WriteLine(strPart)
Next

I hope this helps also?

Cor
 
Using regular expressions - put the imports stmt at the top of the
module:
imports System.Text.RegularExpressions
..
In your code:
dim re as new Regex("(?<num>\d+[.,]\d*|d+|[.,]\d+)")
dim m as match = re.match("abcdefg 123,45 ijklmn")
dim numStr as string

numStr=m.value
or
numstr = m.groups(1).value
or
numstr = m.groups("num").value

The Regex matches numbers in the following format:
x.
x.y
.y
x
where x or y are consecutive digits from 0 to 9.
The 3 numStr assignments are several ways to extract the number. The
last uses the name assigned to the matched string which is the first
part of the Regex pattern ;?<num>. This is optional, but as you take
time to learn regular expressions there is great power in assigning
names to matches.

Hope this helps.
Greg
 
Back
Top