Split text with '\r\n'

前端 未结 8 405
予麋鹿
予麋鹿 2020-12-28 12:42

I was following this article

And I came up with this code:

string FileName = \"C:\\\\test.txt\";

using (StreamReader sr = new StreamReader(FileNam         


        
相关标签:
8条回答
  • 2020-12-28 12:47

    I think the problem is in your text file. It's probably already split into too many lines and when you read it, it "adds" additional \r and/or \n characters (as they exist in file). Check your what is read into text variable.

    The code below (on a local variable with your text) works fine and splits into 2 lines:

    string[] stringSeparators = new string[] { "\r\n" };
    string text = "somet interesting text\nsome text that should be in the same line\r\nsome text should be in another line";
    string[] lines = text.Split(stringSeparators, StringSplitOptions.None);
    
    0 讨论(0)
  • 2020-12-28 12:55

    In Winform App(C#):

    static string strFilesLoc = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), @"..\..\")) + "Resources\\";
        public static string[] GetFontFamily()
                {
                    var result = File.ReadAllText(strFilesLoc + "FontFamily.txt").Trim();
                    string[] items = result.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                    return items;
                }
    

    In-text file(FontFamily.txt):
    Microsoft Sans Serif
    9
    true

    0 讨论(0)
  • 2020-12-28 12:58

    Reading the file is easier done with the static File class:

    // First read all the text into a single string.
    string text = File.ReadAllText(FileName);
    
    // Then split the lines at "\r\n".   
    string[] stringSeparators = new string[] { "\r\n" };
    string[] lines = text.Split(stringSeparators, StringSplitOptions.None);
    
    // Finally replace lonely '\r' and '\n' by  whitespaces in each line.
    foreach (string s in lines) {
        Console.WriteLine(s.Replace('\r', ' ').Replace('\n', ' '));
    }
    

    Note: The text might also contain vertical tabulators \v. Those are used by Microsoft Word as manual linebreaks.

    In order to catch any possible kind of breaks, you could use regex for the replacement

    Console.WriteLine(Regex.Replace(s, @"[\f\n\r\t\v]", " "));
    
    0 讨论(0)
  • 2020-12-28 13:00

    This worked for me.

    string stringSeparators = "\r\n";
    string text = sr.ReadToEnd();
    string lines = text.Replace(stringSeparators, "");
    lines = lines.Replace("\\r\\n", "\r\n");
    Console.WriteLine(lines);
    

    The first replace replaces the \r\n from the text file's new lines, and the second replaces the actual \r\n text that is converted to \\r\\n when the files is read. (When the file is read \ becomes \\).

    0 讨论(0)
  • 2020-12-28 13:03

    The problem is not with the splitting but rather with the WriteLine. A \n in a string printed with WriteLine will produce an "extra" line.

    Example

    var text = 
      "somet interesting text\n" +
      "some text that should be in the same line\r\n" +
      "some text should be in another line";
    
    string[] stringSeparators = new string[] { "\r\n" };
    string[] lines = text.Split(stringSeparators, StringSplitOptions.None);
    Console.WriteLine("Nr. Of items in list: " + lines.Length); // 2 lines
    foreach (string s in lines)
    {
       Console.WriteLine(s); //But will print 3 lines in total.
    }
    

    To fix the problem remove \n before you print the string.

    Console.WriteLine(s.Replace("\n", ""));
    
    0 讨论(0)
  • 2020-12-28 13:05

    Following code gives intended results.

    string text="some interesting text\nsome text that should be in the same line\r\nsome 
    text should be in another line"
    var results = text.Split(new[] {"\n","\r\n"}, StringSplitOptions.None);
    
    0 讨论(0)
提交回复
热议问题