如何在 C# 中使用 String.Split 分隔字符串How to separate strings using String.Split in C#
01/03/2018
本文内容
String.Split 方法通过基于一个或多个分隔符拆分输入字符串来创建子字符串数组。The String.Split method creates an array of substrings by splitting the input string based on one or more delimiters. 此方法通常是分隔字边界上的字符串的最简单方法。This method is often the easiest way to separate a string on word boundaries. 它也用于拆分其他特定字符或字符串上的字符串。It's also used to split strings on other specific characters or strings.
备注
本文中的 C# 示例运行在 Try.NET 内联代码运行程序和演练环境中。The C# examples in this article run in the Try.NET inline code runner and playground. 选择“运行”按钮以在交互窗口中运行示例。Select the Run button to run an example in an interactive window. 执行代码后,可通过再次选择“运行”来修改它并运行已修改的代码。Once you execute the code, you can modify it and run the modified code by selecting Run again. 已修改的代码要么在交互窗口中运行,要么编译失败时,交互窗口将显示所有 C# 编译器错误消息。The modified code either runs in the interactive window or, if compilation fails, the interactive window displays all C# compiler error messages.
下方代码将一个常用短语拆分为一个由每个单词组成的字符串数组。The following code splits a common phrase into an array of strings for each word.
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
foreach (var word in words)
{
System.Console.WriteLine($"");
}
分隔符的每个实例都会在返回的数组中产生一个值。Every instance of a separator character produces a value in the returned array. 连续的分隔符将生成空字符串作为返回的数组中的值。Consecutive separator characters produce the empty string as a value in the returned array. 下面的示例介绍如何创建空字符串,该示例使用空格字符作为分隔符。You can see how an empty string is created in the following example, which uses the space character as a separator.
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
foreach (var word in words)
{
System.Console.WriteLine($"");
}
该行为可以更容易地用逗号分隔值 (CSV) 文件之类的格式表示表格数据。This behavior makes it easier for formats like comma-separated values (CSV) files representing tabular data. 连续的逗号表示空白列。Consecutive commas represent a blank column.
You can pass an optional StringSplitOptions.RemoveEmptyEntries parameter to exclude any empty strings in the returned array. 要对返回的集合进行更复杂的处理,可使用 LINQ 来处理结果序列。For more complicated processing of the returned collection, you can use LINQ to manipulate the result sequence.
String.Split can use multiple separator characters.
下面的示例使用空格、逗号、句点、冒号和制表符作为分隔字符,这些分隔字符在数组中传递到 Split。The following example uses spaces, commas, periods, colons, and tabs as separating characters, which are passed to Split in an array .
代码底部的循环显示返回数组中的每个单词。The loop at the bottom of the code displays each of the words in the returned array.
char[] delimiterChars = { ' ', ',', '.', ':', '\t' };
string text = "one\ttwo three:four,five six seven";
System.Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(delimiterChars);
System.Console.WriteLine($"{words.Length} words in text:");
foreach (var word in words)
{
System.Console.WriteLine($"");
}
任何分隔符的连续实例都会在输出数组中生成空字符串:Consecutive instances of any separator produce the empty string in the output array:
char[] delimiterChars = { ' ', ',', '.', ':', '\t' };
string text = "one\ttwo :,five six seven";
System.Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(delimiterChars);
System.Console.WriteLine($"{words.Length} words in text:");
foreach (var word in words)
{
System.Console.WriteLine($"");
}
String.Split 可采用字符串数组(充当用于分析目标字符串的分隔符的字符序列,而非单个字符)。String.Split can take an array of strings (character sequences that act as separators for parsing the target string, instead of single characters).
string[] separatingStrings = { "<
string text = "one<
System.Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(separatingStrings, System.StringSplitOptions.RemoveEmptyEntries);
System.Console.WriteLine($"{words.Length} substrings in text:");
foreach (var word in words)
{
System.Console.WriteLine(word);
}
请参阅See also