0

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

4

4 回答 4

7

您真的不需要检查,因为File.ReadAllLines()无论行数如何,总是会返回一个字符串数组。您可以利用该行为,只需将返回的数组与您选择的分隔符连接起来。

string singleLine = string.Join(" ", File.ReadAllLines("filepath"));
于 2010-05-18T07:49:16.457 回答
0
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;
}
于 2010-05-18T07:50:13.553 回答
0

尝试类似的事情(取决于你如何对待“线条”):

System.IO.File.ReadAllText(path).Replace("\n\r", "");
于 2010-05-18T07:50:58.577 回答
0

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

string[] lines = File.ReadAllLines("myfile.txt");
string myLine = String.Join(";", lines);
于 2010-05-18T07:51:20.493 回答