本文實例講述了C#統計字符串中數字個數的方法。分享給大家供大家參考。具體實現方法如下:
// DigitCounter.cs// 編譯時使用:/target:libraryusing System; // 聲明與 Factorial.cs 中的命名空間相同的命名空間。這樣僅允許將 // 類型添加到同一個命名空間中。namespace Functions { public class DigitCount { // NumberOfDigits 靜態方法計算 // 傳遞的字符串中數字字符的數目: 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; } }}
希望本文所述對大家的C#程序設計有所幫助。
新聞熱點
疑難解答