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

前端 未结 4 527
挽巷
挽巷 2020-12-15 15:10

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

相关标签:
4条回答
  • 2020-12-15 16:05

    Try the following

    string str = ...;
    bool isLetter = !String.IsNullOrEmpty(str) && Char.IsLetter(str[0]);
    
    0 讨论(0)
  • 2020-12-15 16:08

    Try the following

    bool isValid = char.IsLetter(name.FirstOrDefault());
    
    0 讨论(0)
  • 2020-12-15 16:09
    return (myString[0] >= 'A' && myString[0] <= 'Z') || (myString[0] >= 'a' && myString[0] <= 'z')
    
    0 讨论(0)
  • 2020-12-15 16:09

    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).

    0 讨论(0)
提交回复
热议问题