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#?
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.
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.
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.
Try the following
string str = ...; bool isLetter = !String.IsNullOrEmpty(str) && Char.IsLetter(str[0]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With