C#统计字符串中数字的个数的代码

329 阅读1分钟
将代码过程常用的代码片段珍藏起来,如下代码是关于C#统计字符串中数字的个数的代码,应该对小伙伴们有较大好处。 

using System; 

namespace Functions 
{
    public class DigitCount 
    {
        public static int NumberOfDigits(string theString) 
        {
            int count = 0; 
            for ( int i = 0; i < theString.Length; i++ ) 
            {
                if ( Char.IsDigit(theString[i]) ) 
                {
                    count++; 
                }
            }
            return count;
        }
    }
}