列子一:
class TestStringSplit { static void Main() { char[] delimiterChars = { ' ', ',', '.', ':', '/t' }; string text = "one/ttwo three:four,five six seven"; System.Console.WriteLine("Original text: '{0}'", text); string[] words = text.Split(delimiterChars); System.Console.WriteLine("{0} words in text:", words.Length); foreach (string s in words) { System.Console.WriteLine(s); } } }
输出
Original text: 'one two three:four,five six seven' 7 words in text: one two three four five six seven
例子二:
char[] delimit = new char[] { ' ' }; string s10 = "The cat sat on the mat."; foreach (string substr in s10.Split(delimit)) { System.Console.WriteLine(substr); }
此代码将在单独的行上输出每个单词,如下所示:
The
cat
sat
on
the
mat.