How to get the count of only special character in a string using Regex?

前端 未结 6 576
轮回少年
轮回少年 2021-01-29 07:44

If my input string is ~!@#$%^&*()_+{}:\"<>?

How do I get the count of each special character using Regex? For example:

Regex.Match         


        
6条回答
  •  死守一世寂寞
    2021-01-29 08:28

    The non-regex way (which sounds much easier) it to make a list of characters you want to check and use Linq to find the count of those characters.

    string inputString = "asdf1!%jkl(!*";
    
    List charsToCheckFor = new List() { '!', '@', '#', ..... };
    
    int charCount = inputString.Count(x => charsToCheckFor.Contains(x));
    

    I am making you write in all the characters you need to check for, because you need to figure out what you want.

提交回复
热议问题