简体   繁体   中英

C# StreamReader ReadLine String doesn't add BreakLine

I want to add a breakline to a string. I'm using the streamreader with the readline but it doesn't make the breakline. Here's the code:

        StreamReader re;
        re = new StreamReader("ficheiro.txt", Encoding.UTF8);
        string input = null;
        string textoCompleto = "";
        while ((input = re.ReadLine()) != null)
        {
            textoCompleto += input.ToString();
        }
        listBoxTextoOriginal.Items.Add(textoCompleto);
        string[] arrayPalavras = textoCompleto.Split(' ');
        lblContadorPalavras.Text = arrayPalavras.Length.ToString();
        Array.Sort(arrayPalavras);
        for (int i = 0; i < arrayPalavras.Length; i++)
        {
            listBoxPalavrasOrdenadas.Items.Add(arrayPalavras[i]);
        }

Here is the ficheiro.txt file:

"Estou hoje perplexo, como quem pensou e achou e esqueceu.

Estou hoje dividido entre a lealdade que devo

A Tabacaria do outro lado da rua, como coisa real por fora,

E a sensacao de que tudo e sonho, como coisa real por dentro."

and here is what is doing right now: 在此处输入图片说明

What do I have to do to make this work? Also I have to use the readline because is a exercise to school

StreamReader.ReadLine does not return a string that contains the linebreak that follows, it returns a string that contains everything on the line right up to that linebreak .

When you then concatenate all the strings into one big string, you're losing the line breaks.

Any reason why you're not just replacing the whole loop with this?

textoCompleto = re.ReadToEnd();

This will in fact return the whole file as one string, complete with the linebreaks.

If you can't, or won't, do that, you should make sure you append a newline in your loop:

textoCompleto += input + Environment.NewLine;

Note, I think what you're really after is this:

using (var re = new StreamReader("ficheiro.txt", Encoding.UTF8))
{
    string input;
    while ((input = re.ReadLine()) != null)
        listBoxTextoOriginal.Items.Add(input);
}

This will:

  • Correctly dispose of the StreamReader when you're done with it (see using statement and IDisposable ]
  • Not build a string with all the lines from the file
  • Instead add each line of the file as a unique item to the listbox

Also note that if this wasn't an exercise with arbitrary rules you could do this instead of your whole code:

listBoxTextoOriginal.Items.Addrange(File.ReadAllLines("ficheiro.txt", Encoding.UTF8));

Although it can be fixed, what you are doing is way to much work:

var fileContents = File.ReadAllText("ficheiro.txt", Encoding.UTF8);

listBoxTextoOriginal.Items.Add(fileContents);

There you go. Linebreaks are still what they were in the file.

Well, ReadLine() method doesn't insert new lines anywhere, it just reads line per line the stream. If you want your textoCompleto variable to have line breaks wherever the txt file has, you could simply read it all in just once, for example using

 textoCompleto = re.ReadToEnd();

This will read the entire stream and put its content in textoCompleto.

Anyway, if you must use ReadLine() method, you have just to do this:

while ((input = re.ReadLine()) != null)
{
    textoCompleto += input.ToString() + "\r\n";
}

\\r and \\n are escape characters, that means they won't be interpreted as they are, but the will mean something particular: \\n is a line break and \\r is a Carriage Return... Together they are CRLF, CarrierReturn-LineFeed, or, as we say, a line break. Good luck! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM