Guf*_*ffa 1328
要拆分字符串,您需要使用带有字符串数组的重载:
string[] lines = theText.Split(
new[] { Environment.NewLine },
StringSplitOptions.None
);
Run Code Online (Sandbox Code Playgroud)
编辑:
如果要在文本中处理不同类型的换行符,可以使用匹配多个字符串的功能.这将在任何类型的换行符上正确分割,并在文本中保留空行和间距:
string[] lines = theText.Split(
new[] { "\r\n", "\r", "\n" },
StringSplitOptions.None
);
Run Code Online (Sandbox Code Playgroud)
Clé*_*ent 111
怎么用StringReader?
using (System.IO.StringReader reader = new System.IO.StringReader(input)) {
string line = reader.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
nik*_*d23 44
你应该可以很容易地分割你的字符串,如下所示:
aString.Split(Environment.NewLine.ToCharArray());
Run Code Online (Sandbox Code Playgroud)
Ste*_*per 29
尽量避免使用string.Split作为一般解决方案,因为在使用该函数的任何地方都会使用更多内存 - 原始字符串和拆分副本,都在内存中.请相信我,当你开始扩展时,这可能是一个问题 - 运行一个处理100MB文件的32位批处理应用程序,你将在八个并发线程中废弃.不是说我以前去过那里......
相反,使用这样的迭代器;
public static IEnumerable<string> SplitToLines(this string input)
{
if (input == null)
{
yield break;
}
using (System.IO.StringReader reader = new System.IO.StringReader(input))
{
string line;
while( (line = reader.ReadLine()) != null)
{
yield return line;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这将允许您围绕数据执行更高效的内存循环;
foreach(var line in document.SplitToLines())
{
// one line at a time...
}
Run Code Online (Sandbox Code Playgroud)
当然,如果你想要所有内存,你可以这样做;
var allTheLines = document.SplitToLines.ToArray();
Run Code Online (Sandbox Code Playgroud)
Erw*_*yer 24
根据Guffa的答案,在扩展类中,使用:
public static string[] Lines(this string source) {
return source.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
}
Run Code Online (Sandbox Code Playgroud)
Dan*_*erg 12
从 .NET 6 开始,我们可以使用新的String.ReplaceLineEndings()方法来规范化跨平台行结尾,所以这些天我发现这是最简单的方法:
var lines = input
.ReplaceLineEndings()
.Split(Environment.NewLine, StringSplitOptions.None);
Run Code Online (Sandbox Code Playgroud)
对于字符串变量s:
s.Split(new string[]{Environment.NewLine},StringSplitOptions.None)
Run Code Online (Sandbox Code Playgroud)
这使用了您的环境对行结尾的定义.在Windows上,行结尾是CR-LF(回车,换行)或C#的转义字符\r\n.
这是一个可靠的解决方案,因为如果你重新组合行String.Join,这等于你的原始字符串:
var lines = s.Split(new string[]{Environment.NewLine},StringSplitOptions.None);
var reconstituted = String.Join(Environment.NewLine,lines);
Debug.Assert(s==reconstituted);
Run Code Online (Sandbox Code Playgroud)
什么不该做:
StringSplitOptions.RemoveEmptyEntries,因为这会破坏标记,例如Markdown,其中空行具有语法目的.new char[]{Environment.NewLine}上拆分,因为在Windows上,这将为每个新行创建一个空字符串元素.小智 7
正则表达式也是一个选项:
private string[] SplitStringByLineFeed(string inpString)
{
string[] locResult = Regex.Split(inpString, "[\r\n]+");
return locResult;
}
Run Code Online (Sandbox Code Playgroud)
只是想我会添加我的两位,因为这个问题的其他解决方案不属于可重用的代码分类并且不方便.下面的代码块扩展了string对象,以便在处理字符串时可以使用它作为自然方法.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Collections.ObjectModel;
namespace System
{
public static class StringExtensions
{
public static string[] Split(this string s, string delimiter, StringSplitOptions options = StringSplitOptions.None)
{
return s.Split(new string[] { delimiter }, options);
}
}
}
Run Code Online (Sandbox Code Playgroud)
您现在可以使用.Split()任何字符串中的函数,如下所示:
string[] result;
// Pass a string, and the delimiter
result = string.Split("My simple string", " ");
// Split an existing string by delimiter only
string foo = "my - string - i - want - split";
result = foo.Split("-");
// You can even pass the split options parameter. When omitted it is
// set to StringSplitOptions.None
result = foo.Split("-", StringSplitOptions.RemoveEmptyEntries);
Run Code Online (Sandbox Code Playgroud)
要拆分换行符,只需传递"\n"或"\r\n"作为分隔符参数.
评论: 如果微软实现这种过载会很好.