日期:2014-05-18  浏览次数:20749 次

判断字符串中是否有数字
判断字符串中是否有数字,并统计数字个数!将连续整数输出到一个整数数组中

------解决方案--------------------
[code=c#]
      List <int>list=new List <int>();
      string s = "1sjf36;lj423";
      foreach (char c in s)
        if (c >= 48 && c <= 57)
          list.Add(Convert.ToInt16(c.ToString()));
      int[] a = new int[list.Count];
      for (int i = 0; i < list.Count; i++)
      {
        a[i] = Convert.ToInt32(list[i]);
        Console.WriteLine(a[i]);
      }
[/code]
------解决方案--------------------
C# code

using System.Text.RegularExpressions;

string test = "fdas79fs-afs7df76f6dsa799fd";
int count = Regex.Matches(test, @"\d").Count;
Console.WriteLine("字符串中数字的个数为:{0}", count);
Console.WriteLine("连续整数分别为:");
MatchCollection mc = Regex.Matches(test, @"\d+");
foreach (Match m in mc)
{
    Console.WriteLine(m.Value);
}
Console.Read();

------解决方案--------------------
菜鸟照2楼的谢谢看看:

C# code
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;

namespace IsInt
{
    class Program
    {
        static void Main(string[] args)
        {
            string test = "fdas79fs-afs7df76f6dsa799fd";
            ArrayList integer;
            int No;
            if (IsInteger(test, out No, out integer))
            {
                Console.WriteLine("The Integer Number is:"+No.ToString());
                Console.Write("And the Interger are:");
                StringBuilder sb = new StringBuilder();
                foreach (object b in integer)
                {
                    sb.Append(b.ToString() + ",");
                }
                sb.Remove(sb.Length - 1, 1);
                Console.WriteLine(sb.ToString());
            }
            else
            {
                Console.WriteLine("No Integer!");
            }
        }

        #region 判断是否含有数字
        /// <summary>
        /// 判断是否含有数字
        /// </summary>
        /// <param name="str">要判断的字符串</param>
        /// <param name="number">输出数字个数</param>
        /// <param name="integer">输出数字集合</param>
        /// <returns>是否有数字</returns>
        public static bool IsInteger(string str, out int number,out ArrayList integer)
        {
            bool IsInt;
            if (Regex.IsMatch(str, @"\d"))
            {
                IsInt = true;
                number = Regex.Matches(str, @"\d+").Count;                
                integer = new ArrayList();
                MatchCollection mc = Regex.Matches(str, @"\d+");                
                foreach (Match b in mc)
                {                   
                    integer.Add(b);
                }
            }
            else
            {
                number = 0;
                IsInt = false;
                integer = null;
            }
            return IsInt;
        }
        #endregion
    }
}
/*
The Integer Number is:5
And the Interger are:79,7,76,6,799
请按任意键继续. . .
*/