c#統計字符串中數字字符的個數
題目描述
假設有一個GetNumber方法(參數為字符串strSource),編寫一個靜態方法可以用來統計字符串strSource中數字字符的個數。
輸入
輸入一個字符串strSource
輸出
strSource字符串中數字字符的個數
樣例輸入
style="color:#333333">asffkl8asjkfjklas3jdf9lkj!
樣例輸出
3
using System;
using System.Collections;namespace sample
{class Program{static int GetNumber(string strSource){int num=0;for (int i = 0; i < strSource.Length; i++){if(strSource[i] >= '0' && strSource[i] <= '9'){num++;}}return num;}static void Main(string[] args){string strSource = Console.ReadLine();Console.WriteLine(GetNumber(strSource));}}
}
?