Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check first character of a string if a letter, any letter in C#

I want to take a string and check the first character for being a letter, upper or lower doesn't matter, but it shouldn't be special, a space, a line break, anything. How can I achieve this in C#?

like image 255
korben Avatar asked Aug 24 '10 19:08

korben


People also ask

How do you check if the first character of a string is a letter in c?

strcmp compares the whole string, so it will only return true if the string you're passing is "/". You can look at strncmp instead, or compare only one character ( if (str[0] == '/') ), instead of a string.

How do you check if a string has a certain letter?

Use the String. includes() method to check if a string contains a character, e.g. if (str. includes(char)) {} . The include() method will return true if the string contains the provided character, otherwise false is returned.

How do you check if the first letter of a string is?

Using the isDigit() method Therefore, to determine whether the first character of the given String is a digit. The charAt() method of the String class accepts an integer value representing the index and returns the character at the specified index.


1 Answers

Try the following

string str = ...; bool isLetter = !String.IsNullOrEmpty(str) && Char.IsLetter(str[0]); 
like image 66
JaredPar Avatar answered Oct 11 '22 03:10

JaredPar