String containing digit or Alphabet
-
I have a variable string input How do i detect if variable has digit in it or an alphabet.
o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°
t4urean wrote:
How do i detect if variable has digit in it or an alphabet.
In terms of an exclusive or? To check, if the string is a number:
int i = int.TryParse(input)
If it's just one digit:int i = int.TryParse(input)
and theni =< 9 && i >= 0
If it contains one or more alphabets:bool b = Regex.IsMatch(input, "[a-zA-Z]")
regards -
I have a variable string input How do i detect if variable has digit in it or an alphabet.
o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°
Hi, you can walk the individual characters of the string:
for (int i=0; i
or equivalently (unless you want to change c, wouldnt work here):
foreach (char c in input) {
... do something with c
}and you can use one of many many methods available in String class,
such as IndexOf() and Contains()May I suggest you at least read the MSDN documentation on String class;
or better yet work your way through a introductory C# book.:)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] }
catch { [Google] }
-
I have a variable string input How do i detect if variable has digit in it or an alphabet.
o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°
You can use regex, or iterate over the chars and use things like char.IsDigit.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )