我想在类似如下的字符串中将用[]括起来的字符串提取出来,得到一个字符串数组,请教各位要如何才能实现?
多谢!字符串:单据编号为:[单据头.单据编号],制单日期为:[单据头.业务日期],制单人是:[单据头.制单人],请审核.希望得到如下的数组:
[[单据头.单据编号],[单据头.业务日期],[单据头.制单人]]

解决方案 »

  1.   

    c#的有, str.IndexOf()
    如果没有固定的长度也不是很好取 
      

  2.   

    bool ignore = false;
    string s = "";
    List<string> list = new List<string>();
    string source = "单据编号为:[单据头.单据编号],制单日期为:[单据头.业务日期],制单人是:[单据头.制单人],请审核";
    for (int i = 0; i < source.Length; i++)
    {
        if (source[i] == "[") { s = ""; ignore = true; continue; }
        if (source[i] == "]") { list.Add(s); s = ""; ignore = false; continue; }
        if (source[i] == "," && (!ignore)) { list.Add(s); s = ""; continue; }
        s += source[i];    
    }
    string[] array = list.ToArray();
      

  3.   

    bool ignore = false;
    string s = "";
    List<string> list = new List<string>();
    string source = "单据编号为:[单据头.单据编号],制单日期为:[单据头.业务日期],制单人是:[单据头.制单人],请审核";
    for (int i = 0; i < source.Length; i++)
    {
        if (source[i] == "[") { s = ""; ignore = true; continue; }
        if (source[i] == "]") { list.Add(s); s = ""; ignore = false; continue; }
        if (ignore) s += source[i];    
    }
    string[][] array = list.Select(x => x.Split('.').ToArray()).ToArray();
      

  4.   


    public static void RunSnippet()
    {
    string str = "单据编号为:[单据头.单据编号],制单日期为:[单据头.业务日期],制单人是:[单据头.制单人],请审核";
    StringBuilder sb = new StringBuilder();
    for(int i=0; i<str.Length; i++)
    {
    if(str[i] == '[')
    {
    sb.Append(str.Substring(i, str.IndexOf(']', i) - i + 1)).Append(',');
    }
    }
    sb.ToString().Split(',');
    }
      

  5.   


                string str = "单据编号为:[单据头.单据编号],制单日期为:[单据头.业务日期],制单人是:[单据头.制单人],请审核";
                StringBuilder sb = new StringBuilder("[");
                foreach (Match match in Regex.Matches(str, @"\[[^]]*?\]"))
                {
                    sb.Append(match.Value + ",");
                }
                sb.Remove(sb.Length - 1, 1);
                sb.Append("]");
                Console.WriteLine(sb.ToString());
      

  6.   

     string str = "单据编号为:[dbd.001],制单日期为:[dbd.002],制单人是:[dbd.003],请审核.";
                int startIndex = 0;
                int endIndex = 0;
                int length = str.Split('[').Length;
                string[] Answer = new string[length];
                for (int i = 0; i < str.Split('[').Length; i++)
                {
                    startIndex=str.IndexOf("[");
                    endIndex= str.IndexOf("]")-str.IndexOf("[")+1;
                    Answer[i] = str.Substring(str.IndexOf("["), endIndex);
                    str.Remove(startIndex, endIndex);
                }            Answer 数组 是不是你想要的
      

  7.   

            string str = "单据编号为:[单据头.单据编号],制单日期为:[单据头.业务日期],制单人是:[单据头.制单人],请审核.";
            Regex regex = new Regex(@"[^\]]+?(?=\[|$)");
            str = regex.Replace(str, "");
            string[] result = Regex.Split(str, @"(?<!^)(?=\[)");
            str = "[" + string.Join(",", result) + "]";
            Response.Write(str);