有这么几个字符串:
string str1=“《红与黑》第90章第37页第30行";    //这个字符串要提取出来:90,37,30也就是说提取数字
string str2="《红与黑》第90章第70行";          //这个也是提取90,70数字,我改怎样吧这些数字从字符串中提取出来呢?
并且我知道我提取的数字对应的是章,还是页,还是行????

解决方案 »

  1.   

    用ToCharArray()一个一个字符取出来。
    然后循环下看它们的ASCII码是不是在48~57之间就可以了 
      

  2.   

    正则表达式,写了段代码:
    string str = "《红与黑》第90章第37页第30行";
    List< KeyValuePair<int, string>> list = new List<KeyValuePair<int,string>>();
    foreach (Match m in Regex.Matches(str, "([\\d]+)([行|页|章])"))
    {
        list.Add(new KeyValuePair<int, string>(int.Parse(m.Groups[1].Value), m.Groups[2].Value));
    }//Console.WriteLine("With 行|页|章");
    foreach (KeyValuePair<int, string> k in list)
        Console.Write("{0}({1}), ", k.Key, k.Value);Console.WriteLine();
    //Console.WriteLine("Only number");
    foreach (KeyValuePair<int, string> k in list)
        Console.Write("{0}, ", k.Key);输出结果为
    90(章), 37(页), 30(行),
    90, 37, 30,
      

  3.   


    try {
    Regex regexObj = new Regex(@"\d+");
    Match matchResults = regexObj.Match(subjectString);
    while (matchResults.Success) {
    // matched text: matchResults.Value
    // match start: matchResults.Index
    // match length: matchResults.Length
    matchResults = matchResults.NextMatch();

    } catch (ArgumentException ex) {
    // Syntax error in the regular expression
    }
      

  4.   

    ^[1-9]\d\u0000*$  
    正则表达式支持unicode字符 
    \u0000后面是四位十六位数字 
    汉字编码
      

  5.   

    string str1="《红与黑》第90章第37页第30行";
    Console.WriteLine(Regex.Match(str1, "\\d+(?=章)").Value+"章");
    Console.WriteLine(Regex.Match(str1, "\\d+(?=页)").Value+"页");
    Console.WriteLine(Regex.Match(str1, "\\d+(?=行)").Value+"行");
      

  6.   

    using System.Text.RegularExpressions;
      

  7.   

       Regex r = new Regex(@"\d+[章|页|行]");
            string input = "红与黑》第90章第37页第30行";        string results = "";
            MatchCollection mc = r.Matches(input);
            for (int i = 0; i < mc.Count; i++)
            {
                results += mc[i].Value+"<br>";
            }
            Response.Write(results);
      

  8.   

    string str1="《红与黑》第90章第37页第30行";
                char[] chars = str1.ToCharArray();            int i;
                for(int j=0;j<chars.Length;j++)
                {
                    if(int.TryParse(chars[j].ToString(),out i))
                    {
                        //用一个字典记录数字出现的位置为数字,再用一个数组记录数字后面的字,例如“章”“页”“行”
                        Console.WriteLine(chars[j].ToString());
                    }
                }
               //循环这个字典,把位置连续的单个数字组合起来,比如说9和0组合成90,然后把”章“等加载后面就知道了
                Console.ReadLine();