how to check if a string is a letter or a number
-
lo; i am reading a string from my comm port, i want to check it wether it is a letter or a number, if it is a letter i need to give an error, if it is a number i want to check if it is smaller then 500 how do i do this? grz & thx
You can use a regular expression with the pattern "^\d{1,3}$" to check if the string contains only digits and not more than three digits. If it does, you can safely use int.Parse to parse it to an integer, and check the value.
--- b { font-weight: normal; }
-
lo; i am reading a string from my comm port, i want to check it wether it is a letter or a number, if it is a letter i need to give an error, if it is a number i want to check if it is smaller then 500 how do i do this? grz & thx
If it's a char, you can use Char.IsDigit and Char.IsLetter. If it's a full string, then try this: int n; if (int.TryParse(str, out n) && n < 500) { } else { // Not a number, or >= 500 }
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
You can use a regular expression with the pattern "^\d{1,3}$" to check if the string contains only digits and not more than three digits. If it does, you can safely use int.Parse to parse it to an integer, and check the value.
--- b { font-weight: normal; }
-
thx, but i have found something else public bool hasNumber(String s) { for (int j = 0; j < s.Length; j++) { if (!Char.IsDigit(s,j)) { return false; } } return true; } and to know wether it is a letter, just the oppesit :p thx though