How to find whether a string contains any of the special characters?

后端 未结 10 1329
春和景丽
春和景丽 2020-12-10 01:43

I want to find whether a string contains any of the special characters like !,@,#,$,%,^,&,*,(,)....etc.

How can I do that without looping thorugh all the charact

10条回答
  •  忘掉有多难
    2020-12-10 01:43

    Linq is the new black.

    string.Any(c => char.IsSymbol(c));
    

    For IsSymbol(), valid symbols are members of UnicodeCategory.

    • Letterlike symbols, which include a set of mathematical alphanumeric symbols as well as symbols such as ℅, №, and ™
    • Number forms, such as subscripts and superscripts
    • Mathematical operators and arrows
    • Geometric symbols
    • Technical symbols
    • Braille patterns
    • Dingbats

    Edit:

    This does not catch ALL characters. This may supplement:

    string.Any(c => !char.IsLetterOrDigit(c));
    

提交回复
热议问题