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

前端 未结 5 506
感情败类
感情败类 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: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)

提交回复
热议问题