How to determinatel if a string is a number

  • Thread starter Thread starter ad
  • Start date Start date
A

ad

I want user input a string to a text box, like "101",
How can I determinate if the string that a user input is composite of
digital?
 
ad,

You can use the static TryParse method on the Double structure. This
will allow you to try and determine if the string contains a number.

Hope this helps.
 
ad said:
I want user input a string to a text box, like "101",
How can I determinate if the string that a user input is composite of
digital?

I can imagine two useful ways. First, you could use the Int32.Parse
method to try the conversion. If it throws an exception, you know that
there's something wrong with the string:

...
try {
int val = int.Parse(myString);
}
catch (Exception e) {
// not an integer

// Note: it might be nicer to catch the three exceptions that are
// documented to be thrown by the Parse method.
}
...

The same approach can be coded a lot cleaner in .NET 2, where the
TryParse method has been added. Like this:

...
int result;
if (int.TryParse(myString, out result))
// now result has a valid value


And third, most generally, you can simply analyze the string to see what
it contains. This approach is obviously more flexible than the others,
because you can use it for more complicated strings as well. A regular
expression is good for this kind of check:

...
if (Regex.Match(myString, "^\d+$").Success)
// now you know that the string has only digits


Hope this helps!


Oliver Sturm
 
If you're using .NET 2.0 use the TryParse method of Double/Int etc.

If you're using .NET 1.1 you could use the int.Parse method and wrap it in a
try catch or use a regex to determine if it is a number.

An example regex:

string inputString = "1034";
bool isNumber = Regex.IsMatch(inputString, @"^\d+$");

If you want to allow for a decimal point then use a regex like
@"^\d*(\.\d+)?$"

Hope this helps.
 
I use
if (Int16.TryParse(myString,Int16))
{....}

But can't compile.
Could some one give me an example?

Nicholas Paldino said:
ad,

You can use the static TryParse method on the Double structure. This
will allow you to try and determine if the string contains a number.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ad said:
I want user input a string to a text box, like "101",
How can I determinate if the string that a user input is composite of
digital?
 
ad,

You need to use it on the double structure, like so:

if (double.TryParse(myString, ref myDouble))

The TryParse method was not added to the Int16 structure until .NET 2.0.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ad said:
I use
if (Int16.TryParse(myString,Int16))
{....}

But can't compile.
Could some one give me an example?

Nicholas Paldino said:
ad,

You can use the static TryParse method on the Double structure. This
will allow you to try and determine if the string contains a number.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ad said:
I want user input a string to a text box, like "101",
How can I determinate if the string that a user input is composite of
digital?
 
Back
Top