Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function to check if a string is a number

Status
Not open for further replies.

shresthaal

Programmer
Jun 8, 2005
62
US
is there a function in the vb.net library which can tell if a string is a number or not

eg.
dim A as string
A = "11"

the function should return true for A = "11" and false for A = "12a"

 
There's the old IsNumeric function from the VisualBasic class


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
dim a as string
a = "11"
if IsNumeric(a) then
messagebox.show("yes")
else
messagebox.show("no")
end if


Hope this helps.

[vampire][bat]
 
Stealing" earthandfire's suggestion, do also try with "numbers" like this:

[tt]dim a as string
a = "1E1" ' <- note the letter E ("Scientific" notation)
if IsNumeric(a) then
messagebox.show("yes")
else
messagebox.show("no")
end if[/tt]

If the above scenario is a possibility, then I'd suggest other approaches than IsNumeric. Combine IsNumeric with looping the string, Regex ... Here's one thread with some discussions and suggestions thread796-1104616

(here's discussion from the VB5/6 forum with some history too, should it be interesting thread222-1071392)

Here's an attempt that might work, if the string must contain only digits.

[tt] Dim strNum As String = "234E34"
Dim lngCount As Long
Dim lngLen As Long = strNum.Length - 1

For lngCount = 0 To lngLen
If (Not Char.IsDigit(strNum, lngCount)) Then
MessageBox.Show("contains text")
End If
Next lngCount[/tt]

Roy-Vidar
 
Private Function Parser(byVal str as String) as Boolean
Try
Double.Parse(str)
Return True
Catch fe as FormatException
Return False
End Try
End Function

Works with scientific notation as well as regular numbers.

Tranman


"Adam was not alone in the Garden of Eden, however,...much is due to Eve, the first woman, and Satan, the first consultant." Mark Twain
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top