how to check first character of a string if a lett

2020-02-09 23:47发布

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

thanks in advance, hope this is easy for someone

4条回答
Viruses.
2楼-- · 2020-02-10 00:19
return (myString[0] >= 'A' && myString[0] <= 'Z') || (myString[0] >= 'a' && myString[0] <= 'z')
查看更多
The star\"
3楼-- · 2020-02-10 00:20

Try the following

bool isValid = char.IsLetter(name.FirstOrDefault());
查看更多
beautiful°
4楼-- · 2020-02-10 00:23

Try the following

string str = ...;
bool isLetter = !String.IsNullOrEmpty(str) && Char.IsLetter(str[0]);
查看更多
太酷不给撩
5楼-- · 2020-02-10 00:36

You should look up the ASCII table, a table which systematically maps characters to integer values. All lower-case characters are sequential (97-122), as are all upper-case characters (65-90). Knowing this, you do not even have to cast to the int values, just check if the first char of the string is within one of those two ranges (inclusive).

查看更多
登录 后发表回答