determining if a string contains an int
-
is there any way within the .net framework to determine if a string contains a numeric value?
The easiest way is to put a try...catch around the conversion:
try {
num = int.Parse(str);
ok = true;
} catch {
ok = false;
}Exceptions are quite expensive, though, so it's more efficient to validate the string before you convert it. You can use the Regex class to check if the string only contains digits, for an example. --- b { font-weight: normal; }
-
is there any way within the .net framework to determine if a string contains a numeric value?
do you mean if the string is a number, or can contain a number somewhere amongst a lot of other characters? For the first, if you want to figure out if a string is a number, I'd use Double.TryParse(string) to figure this out (returns a boolean). (Much more effective then "try-catch"ing) For the latter, I'd use regular expression to match any digits (\d) and then it'll return boolean. (regular expression can also subtract the numbers it finds, if needs be...) I hope this help. --------------------------- 127.0.0.1 - Sweet 127.0.0.1 -- modified at 3:18 Monday 5th September, 2005
-
is there any way within the .net framework to determine if a string contains a numeric value?
[Message Deleted]