我想在string 中取出所有的数字
例如
string tt="1a2s3d4f5g6h7q8你";最后取得结果是 tt=12345678如何实现???

解决方案 »

  1.   

    循环。ASCII来判断。或者直接char比较
      

  2.   

    for (int i=1;i<string.lenght;i++)
    {
     string str=string.substring(i-1,i);
    switch(str)
    {
    case "1":
    tt+=str;
    breack;
    case "2":
    tt+=str;
    breack;
    ...
    ..
    }
    }
      

  3.   

    是变量也可以判断啊.
    tt.indexof(i)属于数字
    s=s + tt.indexof(i)  s是目标字串。
      

  4.   

    to:yh95(元亨95)那如果是要取26个字母呢?再来个区分大小写呢?
    岂不要累死。
      

  5.   

    string tt="1a2s3d4f5g6h7q8你.9a9a9a";
    System.Text.StringBuilder sb=new System.Text.StringBuilder();
    for(int i=0;i<tt.Length;i++)
    {
    if(tt[i]>=48 && tt[i]<=57 || tt[i]=='.')
    {
    sb.Append(tt[i]);
    }
    }
    MessageBox.Show(sb.ToString());你可以添加一个标记变量判断已经添加了一个小数点号.
      

  6.   

    用正则表达式匹配非数字,然后使用regex的replace方法把所有的匹配项替换成"",即空字符串
      

  7.   

    using System;
    using System.Text.RegularExpressions ;namespace ConsoleApplication6
    {
    /// <summary>
    /// Class1 概要説明
    /// </summary>
    class Class1
    {
    /// <summary>
    /// 
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    string temp ="1fy75m02j9";
    string biaozhun=@"[^0-9]";
    temp=Regex.Replace(temp,biaozhun,"");
    System.Console.WriteLine(temp);
    }
    }
    }