Easiest way to split a string on newlines in .NET?
I need to split a string into newlines in .NET and the only way I know of to split strings is with the Split method. However that will not allow me to (easily) split on a newline, so what is the best way to do it?
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[] { "rn", "r", "n" },
StringSplitOptions.None
);
怎么样使用StringReader
?
using (System.IO.StringReader reader = new System.IO.StringReader(input)) {
string line = reader.ReadLine();
}
你应该能够很容易地分割你的字符串,就像这样:
aString.Split(Environment.NewLine.ToCharArray());
链接地址: http://www.djcxy.com/p/27818.html
下一篇: 在.NET中换行符的最简单方法是什么?