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

前端 未结 6 519
轮回少年
轮回少年 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:07

    If you want to follow other approach then you can use.

    string str =  "@123:*&^789'!@#$*()_+=";
                int count = 0;
                foreach (char c in str)
                {
                    if (!char.IsLetterOrDigit(c.ToString(),0))
                    {
                        count++;
                    }
                }
                MessageBox.Show(count.ToString());
    

提交回复
热议问题