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

解决方案 »

  1.   


                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]);
                }
      

  2.   


    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(); 
      

  3.   

    2楼的根据ascii码。三楼的是根据正则表达式。哈哈..
      

  4.   

    支持,正则表达。。多简单啊,就一个\d取所有数字。
    7楼的说发,2楼的只要把foreach里面的哪句改成数组添加元素就行!
      

  5.   


    using System;
    using System.Text.RegularExpressions;public class Test
    {    public static void Main ()
        {       Regex rx = new Regex(@"d+");
           
           // Define some test strings.
           string[] tests = {"asfdad", "1adfg9.99", "0.sdga001", "1ads UdgSD"};
           
           // Check each test string against the regular expression.
           foreach (string test in tests)
           {
               if (rx.IsMatch(test))
               {
                   Console.WriteLine("{0} contains digit.", test);
               }
               else
               {
                   Console.WriteLine("{0} doesn't contain digit.", test);
               }
           }
          
        }

    }
      

  6.   

    菜鸟照2楼的谢谢看看: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
    请按任意键继续. . .
    */
      

  7.   

    把这个String转换为字符数组,遍历之
    这个比较容易入手。
      

  8.   

    C#判断一个string是否为数字 
    http://www.cnblogs.com/zm235/archive/2006/09/23/512742.htmlpublic bool IsNumeric(string val)
    {
    if (val == null)
    {
    return false;
    }
    double d = 0;
    return Double.TryParse(val, System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out d);
    }public bool IsNumeric(string val, ref double d)
    {
    if (val == null)
    {
    return false;
    }
    return Double.TryParse(val, System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out d);
    }
      

  9.   

    这样:
    str2=str.Replace("0","").Replace("1","").Replace("3","").Replace("4","").Replace("5","").Replace("6","").Replace("7","").Replace("8","").Replace("9","");if(str==str2){
        没有数字;
    }
    esle{
        有数字;
         数字的个数是str.length-str2.length;
    }