列子一:
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.
本文通过两个示例展示了如何使用C#语言中的Split方法来拆分字符串。第一个示例使用多种字符作为分隔符,并展示如何将包含这些分隔符的字符串拆分成独立的单词。第二个示例演示了如何通过空格来拆分句子。
458

被折叠的 条评论
为什么被折叠?



