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

前端 未结 5 389
感情败类
感情败类 2021-01-23 21:46

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\         


        
相关标签:
5条回答
  • 2021-01-23 22:37

    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).

    0 讨论(0)
  • 2021-01-23 22:38
    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)

    0 讨论(0)
  • 2021-01-23 22:40

    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" );
    
    0 讨论(0)
  • 2021-01-23 22:43

    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´s code and this do take care of both \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)

    0 讨论(0)
  • 2021-01-23 22:46

    Try

    string[] items = source.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
    
    0 讨论(0)
提交回复
热议问题