class TestStringSplit
{
static void Main()
{
char[] delimiterChars = { ' ', ',', '.', ':', '\t' };
string text = "a\tb c:d,e f g";
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);
}
// Keep the console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
/* Output:
a
b
c
d
e
f
g
*/
As input, Split takes an array of chars that indicate which characters are to be used as delimiters.
本文介绍了一个使用C#进行字符串拆分的例子,演示了如何通过指定分隔符数组来将原始字符串拆分成多个子字符串。这个例子有助于理解如何灵活地处理文本数据。
798

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



