4

i want to read from a text file in C#. But I want all the lines in the file to be concatenated into one line.

for example if i have in the file as

ABCD

EFGH

I need to read ABCDEFGH as one line.

I can do this by reading one line at a time from the file and concatenating that line to a string in a loop. But are there any faster method to do this?

2
  • 1
    What's your purpose? Modifying the file on disc or getting the whole content in a variable? Commented Mar 21, 2012 at 7:32
  • I have a file with thousands of lines, i want to read them as one line and store that line in a variable. Commented Mar 21, 2012 at 7:38

4 Answers 4

9
string.Join(" ", File.ReadAllLines("path"));

Replace " " with "" or any other alternative "line-separator"

Example file:

some line

some other line

and yet another one

With " " as separator: some line some other line and yet another one

With "" as separator: some linesome other lineand yet another one

Sign up to request clarification or add additional context in comments.

3 Comments

Yes i can do this or the replace method, but will that be faster?
Can you explain why this is more efficient?
string.Join allocates smartly and has O(n) complexity, where n is the number of lines. string.Replace has O(m) complexity, which m is the number of characters (total) because it goes through each character and compares it to Environment.NewLine, and we know the m > n (the number of total characters is bigger than the number of lines) - so its simply less to loop through.
6

Use this:

using (System.IO.StreamReader myFile = new System.IO.StreamReader("test.txt")) {
   string myString = myFile.ReadToEnd().Replace(Environment.NewLine, "");
}

1 Comment

Hi it does not remove the new line characters. for example if i have in the file as ABCD EFGH I need to read ABCDEFGH as one line.
1
string file = File.ReadAllText("text.txt").Replace("\r\n", " ");

Comments

0

What is a one line for you?

If you want to put the entire content of a file into a string, you could do

string fileContent  = File.ReadAllText(@"c:\sometext.txt");

If you want your string without newline characters you could do

fileContent = fileContent.Replace(Environment.NewLine, " ");

12 Comments

This will work, but it is less efficient than using File.ReadAllLines and string.Join
@Yorye How big is the difference?
If n is the number of lines, and m is the number of characters in all lines combined, then the difference is multiply by m divide by n. (Since m >= n, it looks promising)
@Yorye Would you care to explain your reasoning. string.Replace just calls some opaque CLR method, so I was not able to compare it with string.Join implementation.
You don't have to look at the disassembly, just think what the logic of the function means. Either put strings end-to-start one after another (resulting with string length x), or go through one long string (with length ~x), comparing each character to a linefeed, and replacing it (maybe even allocating each time a new super-long string - but even if not: tones of unnecessary compares!)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.