比如 txt12 中的 1 的索引,能最简到什么样子呢

解决方案 »

  1.   

    Regex r = new Regex(@"\d");
    int i = 0;
    for (int k = 0; k < txt12.Length; k++)
    {
        if (r.IsMatch(txt12))
           {
               break;
           }
        i++;
    }。
      

  2.   


    /// <summary>
    /// 返回指定字符串第一次出现数字的位置,如果未找到返回-1.
    /// </summary>
    public int IndexOfFirstNumber(string text)
    {
      if (string.IsNullOrEmpty(text))
        return -1;  int i, length = text.length;  for (i = 0; i < length; i++)
      {
        char ch = text[i];
        if (char.IsDigit(ch)) break;
      }  return i == length ? -1 : i;
    }
      

  3.   

    用不着这么麻烦
    Regex r = new Regex(@"\d");
    int index = r.Match( txt12 ).Index; 
      

  4.   

    如果找不到,则得到-1:
           string s = "txt12";
    int index=-1;
    for(int i=0;i<s.Length;i++)
    if (char.IsDigit(s[i]))
    {
    index = i;
    break;
    }
    Response.Write(index);
      

  5.   

    int index = "txt12".IndexOf("1");
      

  6.   

    好吧,没注意不成功的情况
    Regex r = new Regex(@"\d");
    Match m = r.Match( txt12 );
    return m.Success ? m.Index : -1; 
      

  7.   

    int index = "txt12".IndexOf("1");
      

  8.   

    没有的事,大家都是在学习中不断提高的
    不过要学会查文档倒是真的,这些都是在MSDN中可以找到的