tags:

views:

75

answers:

7

I want to run a check on a String right before I append it to a StringBuilder to make sure only numeric characters are in the string. What's a simple way to do that?

+3  A: 

Use Integer.TryParse() it will return true if there are only digits in the string. Int32 max value is 2,147,483,647 so if your value is less then that then your fine. http://msdn.microsoft.com/en-us/library/f02979c7.aspx

You can also use Double.TryParse() which has a max value of 1.7976931348623157E+308 but it will allow a decimal point.

If your looking to get the value that isnt an integer you can always go through the string one at a time

string test = "1112003212g1232";
        int result;
        bool append=true;
        for (int i = 0; i < test.Length-1; i++)
        {
            if(!Int32.TryParse(test.Substring(i,i+1),out result))
            {
                //Not an integer
                append = false;
            }
        }

If append stays true then the string is an integer. Probably a more slick way of doing this but this should work.

Gage
As long as the string isn't larger than the max Int32.
AllenG
That won't work if the string is too long resulting in the integer overflow
Mr. Brownstone
Pretty much I just want to make sure a string only has numeric characters, and IF it doesn't, I need to find the location in that string where the non-numeric character is located, and see what the value is
Scott
Yes I realize thats C#, same basic idea though
Gage
+1  A: 

Pattern matching! See this, this (about.com) and this (VB.NET dev article).

Kyra
+1  A: 

You can use regular expression or Integer.TryParse and I prefer the regular expression check

Kronass
+4  A: 

Use regular expressions:

Dim reg as New RegEx("^\d$")

If reg.IsMatch(myStringToTest) Then
  ' Numeric
Else
  ' Not
End If

UPDATE:

You could also use linq to accomplish the same task if you're doing it in VB.Net 2008/2010.

Dim isNumeric as Boolean = False

Dim stringQuery = From c In myStringToTest 
                          Where Char.IsDigit(c) 
                          Select c

If stringQuery.Count <> myStringToTest.Length Then isNumeric = False
Joel Etherton
Regular Expressions is the more elegant solution IMHO. We use RE for most of our input validation and data scrubbing.
knslyr
+1  A: 

Presuming you're looking at relatively short strings which will never have a number greater than the Max Int32 value, use Gage's solution. If it's a variable length and sometimes you could overflow, use Regex (System.Text.RegularExpressions)

The regex for checking against just numbers is fairly routine: ^[0-9]+$

Check here for a very good explanation of Regex.

AllenG
+2  A: 

If you do not wish to use RegEx, a simple check on each character with char.IsNumber works.

You can combine it with the All extension method (in C#, I don't know how to write it in VB.net):

string value = "78645655";
bool isValid = value.All(char.IsNumber);

Check out other char method, like IsDigit.

Pierre-Alain Vigeant
+1  A: 

2 other compact solutions :

Without LINQ :

Dim foo As String = "10004"
Array.Exists(foo.ToCharArray, Function(c As Char) Not Char.IsNumber(c))

With LINQ (just VB.Net equivalent of the C# version in another answer) :

foo.All(Function(c As Char) Char.IsNumber(c))
Vanmachin