一段这样的数据.A(c),B(n),C(u),D(r),E(t),F(8).......
我想用一个for或foreach循环语句把括号里的值,即(c,n,u,r,t,8....)读出来,如何实现?

解决方案 »

  1.   

    假设A(c),B(n),C(u),D(r),E(t),F(8).......是一个string
    string [] temp = string.Split(',');
    string [] temp1 = new string[temp.length];
    for(int i = 0;i < temp.length;i ++)
    {
    temp[i] = temp[i].substring(2,1);
    }
      

  2.   

    先用Split(',')给分成一个数组,然后对数组中每个串用Substring取第二到第三位的值,
    如果你的位数是不固定的那就用IndexOf取(和)位置,用Substring取两个位置间的串
      

  3.   

    用正则表达式吧。
    string a = "A(c),B(n),C(u),D(r),E(t),F(8)";
    System.Text.RegularExpressions.MatchCollection matchs = System.Text.RegularExpressions.Regex.Matches(a, @"\((?<value>[^\)])\)");
    foreach (System.Text.RegularExpressions.Match match in matchs)
    {
          Response.Write(match.Groups["value"].Value + "<br/>");
    }
      

  4.   

    用正则效率更好些
    string str = "A(c),B(n),C(u),D(r),E(t),F(8),G(10)......";
    string result = "";
    string temp = str.Substring(str.IndexOf("("));
    while(temp.IndexOf("(") >= 0)
    {
    result += temp.Substring(1, temp.IndexOf(")")-1) + ","; 
    temp = temp.Substring((temp.IndexOf("(", 1) < 0) ? 1 : temp.IndexOf("(", 1));
    }
    MessageBox.Show(result);