How to check a string

  • Thread starter Thread starter ad
  • Start date Start date
Hi ad,

What exactly did you mean by "composite of number"?

Could you please describe in more detail what you are trying to accomplish?
 
Hi,

You can use:

bool IsInteger(string s)
{
try
{
int n = int.Parse(s);
return true;
}
catch
{
return false;
}

This will accept a '+' or '-' sign in front of a number. If you don't want
that, you could write your own function:

bool IsInteger(string s)
{
for (int i = 0; i < s.Length; i++)
if (s < '0' || s > '9')
return false;
return true;
}

Check against a regular expression could be another solution.

Regards - Octavio
 
Octavio said:
Check against a regular expression could be another solution.

Yep, and much easier:

if (Regex.IsMatch(myString, "\d+"))
// myString contains at least one character 0-9


Oliver Sturm
 
Back
Top