使用另一个字符串作为分隔符拆分字符串(例如,拆分:从123:1开始)

时间:2017-04-13 06:40:50

标签: c# asp.net vb.net

我通过javascript创建一个字符串数组传递值 “621:1622:1623:1624:1,”

我不知道如何从具有不同功能的字符串数组中取消“:1”或:2

 char[] delimiterChars = { '1' };
 char[] delimiterChars = { ':' };

 string[] words = text.Split(delimiterChars);
 string text = "621:1,622:1,623:1,624:1,";

 string[] words = text.Split(delimiterChars);
 System.Console.WriteLine("{0} words in text:", words.Length);

        string save  = "";
        foreach (string s in words)
        {                 
             System.Console.WriteLine(s);
        }

1 个答案:

答案 0 :(得分:2)

使用String.Split(String[], StringSplitOptions)重载:

string[] delimiterStrings = new []{ ":1," };
string text = "621:1,622:1,623:1,624:1,";
string[] words = text.Split(delimiterStrings, StringSplitOptions.RemoveEmptyEntries);

.net Fiddle

结果:

文字中的4个字: 621 622 623 624