ad wrote:
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
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619
http://www.sturmnet.org/blog