代码之家  ›  专栏  ›  技术社区  ›  ratty

如何在C_中读取文本文件中的多行?

  •  1
  • ratty  · 技术社区  · 14 年前

    我喜欢阅读检查文本有多行或单行,然后我要阅读多行并转换为单行,我该怎么做?

    4 回复  |  直到 14 年前
        1
  •  7
  •   Sky Sanders    14 年前

    你真的不需要检查 File.ReadAllLines() 将始终返回字符串数组,而不考虑行数。您可以利用这种行为,只需使用所选的分隔符加入返回的数组。

    string singleLine = string.Join(" ", File.ReadAllLines("filepath"));
    
        2
  •  0
  •   David Neale    14 年前
    string text = String.Empty;
    if(textbox.Text.Contains(Environment.NewLine))
    {
        //textbox contains a new line, replace new lines with spaces
        text = textbox.Text.Replace(Environment.NewLine, " ");
    }
    else
    {
        //single line - simply assign to variable
        text = textbox.Text;
    }
    
        3
  •  0
  •   UserControl    14 年前

    尝试类似的方法(取决于你如何对待“线”):

    System.IO.File.ReadAllText(path).Replace("\n\r", "");
    
        4
  •  0
  •   Andre    14 年前

    这将从文本文件中读取所有行,并用;作为分隔符将它们联接成一个字符串:

    string[] lines = File.ReadAllLines("myfile.txt");
    string myLine = String.Join(";", lines);