欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#统计字符串中数字个数的方法

程序员文章站 2023-11-12 23:55:04
本文实例讲述了c#统计字符串中数字个数的方法。分享给大家供大家参考。具体实现方法如下: // digitcounter.cs // 编译时使用:/target:...

本文实例讲述了c#统计字符串中数字个数的方法。分享给大家供大家参考。具体实现方法如下:

// digitcounter.cs
// 编译时使用:/target:library
using 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#程序设计有所帮助。