C# 在 .NET 中用换行符拆分字符串的最简单方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1547476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:43:53  来源:igfitidea点击:

Easiest way to split a string on newlines in .NET?

c#.netstringsplit

提问by RCIX

I need to split a string into newlines in .NET and the only way I know of to split strings is with the Splitmethod. However that will not allow me to (easily) split on a newline, so what is the best way to do it?

我需要在 .NET 中将字符串拆分为换行符,而我知道的拆分字符串的唯一方法是使用Split方法。但是,这不允许我(轻松)在换行符上拆分,那么最好的方法是什么?

采纳答案by Guffa

To split on a string you need to use the overload that takes an array of strings:

要拆分字符串,您需要使用采用字符串数组的重载:

string[] lines = theText.Split(
    new[] { Environment.NewLine },
    StringSplitOptions.None
);

Edit:
If you want to handle different types of line breaks in a text, you can use the ability to match more than one string. This will correctly split on either type of line break, and preserve empty lines and spacing in the text:

编辑:
如果要处理文本中不同类型的换行符,可以使用匹配多个字符串的功能。这将在任一类型的换行符上正确拆分,并在文本中保留空行和间距:

string[] lines = theText.Split(
    new[] { "\r\n", "\r", "\n" },
    StringSplitOptions.None
);

回答by nikmd23

You should be able to split your string pretty easily, like so:

您应该能够很容易地拆分字符串,如下所示:

aString.Split(Environment.NewLine.ToCharArray());

回答by Max

I did not know about Environment.Newline, but I guess this is a very good solution.

我不知道 Environment.Newline,但我想这是一个很好的解决方案。

My try would have been:

我的尝试是:

        string str = "Test Me\r\nTest Me\nTest Me";
        var splitted = str.Split('\n').Select(s => s.Trim()).ToArray();

The additional .Trim removes any \r or \n that might be still present (e. g. when on windows but splitting a string with os x newline characters). Probably not the fastest method though.

额外的 .Trim 删除可能仍然存在的任何 \r 或 \n (例如,在 Windows 上但使用 os x 换行符拆分字符串时)。虽然可能不是最快的方法。

EDIT:

编辑:

As the comments correctly pointed out, this also removes any whitespace at the start of the line or before the new line feed. If you need to preserve that whitespace, use one of the other options.

正如评论正确指出的那样,这也会删除行首或新换行符之前的任何空格。如果您需要保留该空格,请使用其他选项之一。

回答by MaciekTalaska

Well, actually split should do:

好吧,实际上 split 应该这样做:

//Constructing string...
StringBuilder sb = new StringBuilder();
sb.AppendLine("first line");
sb.AppendLine("second line");
sb.AppendLine("third line");
string s = sb.ToString();
Console.WriteLine(s);

//Splitting multiline string into separate lines
string[] splitted = s.Split(new string[] {System.Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);

// Output (separate lines)
for( int i = 0; i < splitted.Count(); i++ )
{
    Console.WriteLine("{0}: {1}", i, splitted[i]);
}

回答by Serge Wautier

string[] lines = text.Split(
  Environment.NewLine.ToCharArray(), 
  StringSplitOptions.RemoveEmptyStrings);

The RemoveEmptyStringsoption will make sure you don't have empty entries due to \n following a \r

RemoveEmptyStrings选项将确保你没有空项是由于\ N按照一个\ r

(Edit to reflect comments:) Note that it will also discard genuine empty lines in the text. This is usually what I want but it might not be your requirement.

(编辑以反映评论:)请注意,它还会丢弃文本中的真正空行。这通常是我想要的,但可能不是您的要求。

回答by Erwin Mayer

Based on Guffa's answer, in an extension class, use:

根据 Guffa 的回答,在扩展类中,使用:

public static string[] Lines(this string source) {
    return source.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
}

回答by Colonel Panic

For a string variable s:

对于字符串变量s

s.Split(new string[]{Environment.NewLine},StringSplitOptions.None)

This uses your environment's definition of line endings. On Windows, line endings are CR-LF (carriage return, line feed) or in C#'s escape characters \r\n.

这使用您的环境对行尾的定义。在 Windows 上,行结尾是 CR-LF(回车、换行)或在 C# 的转义字符中\r\n

This is a reliable solution, because if you recombine the lines with String.Join, this equals your original string:

这是一个可靠的解决方案,因为如果您将这些行与 重新组合String.Join,则这等于您的原始字符串:

var lines = s.Split(new string[]{Environment.NewLine},StringSplitOptions.None);
var reconstituted = String.Join(Environment.NewLine,lines);
Debug.Assert(s==reconstituted);


What not to do:

什么不能做:

  • Use StringSplitOptions.RemoveEmptyEntries, because this will break markup such as Markdown where empty lines have syntactic purpose.
  • Split on separator new char[]{Environment.NewLine}, because on Windows this will create one empty string element for each new line.
  • 使用StringSplitOptions.RemoveEmptyEntries,因为这会破坏标记,例如空行具有语法目的的 Markdown。
  • 在 separatornew char[]{Environment.NewLine}上拆分,因为在 Windows 上,这将为每个新行创建一个空字符串元素。

回答by Colonel Panic

Silly answer: write to a temporary file so you can use the venerable File.ReadLines

愚蠢的答案:写入一个临时文件,以便您可以使用古老的 File.ReadLines

var s = "Hello\r\nWorld";
var path = Path.GetTempFileName();
using (var writer = new StreamWriter(path))
{
    writer.Write(s);
}
var lines = File.ReadLines(path);

回答by Clément

What about using a StringReader?

使用一个StringReader怎么样?

using (System.IO.StringReader reader = new System.IO.StringReader(input)) {
    string line = reader.ReadLine();
}

回答by user1964822

Regex is also an option:

正则表达式也是一种选择:

    private string[] SplitStringByLineFeed(string inpString)
    {
        string[] locResult = Regex.Split(inpString, "[\r\n]+");
        return locResult;
    }