How can I convert a string with newlines in it to separate lines?

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

How to convert string that have \r\n to lines?

For example, take this string:

string source = "hello \r\n this is a test \r\n tested";

and how can I convert that to:

string[] lines ;  //lines[0] = hello; //lines[1] = this is a test; //lines[2] = tested ;

回答1:

Use String.Split Method (String[], StringSplitOptions) like this:

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

This one will work regardless of whether the source is written with Windows linebreak \r\n or Unix \n.

As other answers mention, you could use StringSplitOptions.RemoveEmptyEntries instead of StringSplitOptions.None which, as the name says, removes empty strings (" " does not qualify for being empty, only ""does).



回答2:

In addition to string.Split which is mentioned, you can also use Regex.Split, but it's often more useful for more complicated split patterns.

var lines = Regex.Split( source, @"\r\n" );


回答3:

string text = "Hello \r\n this is a test \r\n tested"; string[] lines = text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); foreach (string line in lines) {     System.Diagnostics.Debug.WriteLine(line); }

You can use the overloads of System.String.Split to use a single character, array of characters, or an array of string values as the delimiters for splitting the string. The StringSplitOptions specifies whether or not to keep blank lines, or to remove them. (You would want to keep them for example if you are splitting multi-line text such as a paragraph, but would remove them in an example of a list of names)



回答4:

Try

string[] items = source.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);


回答5:

For clarity I have made another answer.

I have made these two extension methods:

ReadLines will let you read one line at the time without parsing the whole string into an array. ReadAllLines will parse the whole line into an array.

In your case, you can use it like:

var lines = source.ReadAllLines();

I have taken the names from File.ReadLines and File.ReadAllLines so they should be consistent with the rest of .Net framework.

As a side note to comments on my other answer, I have checked StringReader.ReadLine\r\n and \n - but not the old legacy Mac Os etc. (shouldn't be important).

public static class StringExtensions {     public static IEnumerable<string> ReadLines(this string data)     {         using (var sr = new StringReader(data))         {             string line;              while ((line = sr.ReadLine()) != null)                 yield return line;         }     }      public static string[] ReadAllLines(this string data)     {         var res = new List<string>();          using (var sr = new StringReader(data))         {             string line;              while ((line = sr.ReadLine()) != null)                 res.Add(line);         }          return res.ToArray();     } }

(ReadAllLines could have been made with ReadLines.ToArray but I decided that the extra few lines of code was better than an wrapped IEnumerable which takes some processing time)



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!