已知字符串长度为7,其中有一位是字母(不确定位置,但肯定有)
怎样判断此字串中字母的位置?从而截取该串。新手求救!!如:0D4100103  
    004P00103
    0D41S0103

解决方案 »

  1.   


    void Main()
    {
    var list=new List<string>
    {
        "0D4100103",
    "004P00103",
    "0D41S0103"
    };
    list.ForEach(l=>Console.WriteLine(Regex.Match(l,"[a-zA-Z]").Index));
    /*
    1
    3
    1
    */
    }
      

  2.   


    不好意思:
    是变数:0D4100103   
      004P00303
      0041S0603
      

  3.   


               string str = "0D4100103";
                int i = 0;
                foreach (char c in str)
                {
                    i++;
                    if (char.IsLetter(c))
                    {
                        str = str.Remove(i - 1, 1);
                    }
                }
      

  4.   


    using System;
    using System.Text.RegularExpressions;//要用正则表达式必须引用这个命名空间
    using System.Windows.Forms;namespace WindowsApplication3
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                string str = "0D4100103";
                string test = str.Substring(0, Regex.Match(str, "[a-zA-Z]").Index);//前半部份,不包含字母
                string test1 = str.Substring(0, Regex.Match(str, "[a-zA-Z]").Index+1);//前半部份,包含字母
                string test2 = str.Substring(Regex.Match(str, "[a-zA-Z]").Index);//后半部份,包含字母
                string test3 = str.Substring(Regex.Match(str, "[a-zA-Z]").Index + 1);//后半部份,不包含字母
                MessageBox.Show(this, test, "前半部份,不包含字母");
                MessageBox.Show(this, test1, "前半部份,包含字母");
                MessageBox.Show(this, test2, "后半部份,包含字母");
                MessageBox.Show(this, test3, "后半部份,不包含字母");
            }
        }
    }
      

  5.   

     string s = "0D4100103";
                for (int i = 0; i < s.Length; i++)
                {
                    if (s[i] > 'A')
                    {
                        Console.WriteLine(s[i]);
                        Console.WriteLine(i);
                        break;
                    }
                }
                Console.Read();
      

  6.   

     string s = "0D4100103";
                for (int i = 0; i < s.Length; i++)
                {
                    if (s[i] > 'A')
                    {
                        Console.WriteLine(s[i]);
                        Console.WriteLine(i);
                        break;
                    }
                }
                Console.Read();
      

  7.   

     string s = "0D4100103";
                for (int i = 0; i < s.Length; i++)
                {
                    if (s[i] >= 'A')
                    {
                        Console.WriteLine(s[i]);
                        Console.WriteLine(i);
                        break;
                    }
                }
                Console.Read();
      

  8.   

    第三个例子 D   S  是Exception了?如果例子错了……那啥 挨个字符去比对取得'A' 'Z'之间的,就返回下标。
      

  9.   

                string input = "023D4100103";
                Console.WriteLine(Regex.Match(input,@"^\d*").Value.Length);
      

  10.   

    string str = "01D4100103";
                int index = 0 ;
                Regex reg = new Regex("^[a-zA-Z]$");
                for (int i = 0; i < str.Length; i++)
                {                if (reg.IsMatch(str[i].ToString()))
                    {
                        index = i;
                        break;
                    }
                }
                string[] strs = str.Split(str[index]);
                for (int i = 0; i < strs.Length; i++)
                {
                    Console.WriteLine(strs[i]);
                    
                }