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

后端 未结 10 1326
春和景丽
春和景丽 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

    Here is a short solution to check for special chars using LINQ

    private bool ContainsSpecialChars(string value)
    {
            var list = new[] {"~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "+", "=", "\""};
            return list.Any(value.Contains);
    }
    

提交回复
热议问题