30秒学会 C# 片段 · 2017年8月11日

30秒学会 C# 片段 – SplitLines

Splits a multiline string into an array of lines.

Use string.Split() with all forms of the newline separator to split the string into an array of strings.

代码实现

using System.Collections.Generic;

public static partial class _30s 
{
  public static string[] SplitLines(string s)
  {
    return s.Split(new [] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
  }
}

使用样例

string s = "This\nis a\nmultiline\nstring.\n";

_30s.SplitLines(s); // {"This", "is a", "multiline", "string." , ""}