开发者

how to read multi lines in text file in c#? [duplicate]

开发者 https://www.devze.com 2022-12-30 10:12 出处:网络
This question already has answers here: How to read an entire file to a string using C#? (16 answers) Closed 3 years开发者_开发问答 ago.
This question already has answers here: How to read an entire file to a string using C#? (16 answers) Closed 3 years开发者_开发问答 ago.

i like to read check the text has multi line or single line and then i am going to read that multi lines and convert into single line how can i do this?


You really do not need to check as File.ReadAllLines() will always return a string array regardless of the number of lines. You can leverage that behavior and simply join the returned array with your separator of choice.

string singleLine = string.Join(" ", File.ReadAllLines("filepath"));


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;
}


try something like that (depends on how you treat "lines"):

System.IO.File.ReadAllText(path).Replace("\n\r", "");


This will read all the lines from a text file and join them into one string with ; as a separator:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号