以5678开头并以abcd结束的字符串的分割问题56780000hfaihfaabcd,可以分割为0000hfaihfa但是如果5678前面有字符且不是abcd的就不分割,该如何做呢?如56781335678dsfadfabcd,就分割为1335678dsfadf,而不是分割为133和dsfadf请教一个最快的算法。

解决方案 »

  1.   

    string s = "56781335678dsfadfabcd";
    if (s.StartsWith("5678")) s = "abcd" + s;
    if (s.EndsWith("abcd")) s = s + "5678";
    string[] result = s.Split(new string[] { "abcd5678" }, StringSplitOptions.RemoveEmptyEntries);
      

  2.   

    public string GetNewString(string str)
    {
       while(str.StartWith("5678") && str.EndWith("abcd")) //5678开头,abcd结尾
       {
          str=str.Substring(4,str.Length-8);
          while(str.StartWith("abcd5678")) //abcd5678开头
          {
             str=str.Substring(8);
          }
       }
       retur str;   
    }