C# StreamReader ReadLine String doesn't add BreakLine -
i want add together breakline string. i'm using streamreader readline doesn't create breakline. here's 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); (int = 0; < arraypalavras.length; i++) { listboxpalavrasordenadas.items.add(arraypalavras[i]); } here ficheiro.txt file:
"estou hoje perplexo, como quem pensou e achou e esqueceu.
estou hoje dividido entre lealdade que devo
a tabacaria outro lado da rua, como coisa real por fora,
e sensacao de que tudo e sonho, como coisa real por dentro."
and here doing right now:
what have create work? have utilize readline because exercise school
streamreader.readline not homecoming string contains linebreak follows, returns string contains on line right linebreak.
when concatenate strings 1 big string, you're losing line breaks.
any reason why you're not replacing whole loop this?
textocompleto = re.readtoend(); this in fact homecoming whole file 1 string, finish linebreaks.
if can't, or won't, that, should create sure append newline in loop:
textocompleto += input + environment.newline; note, think you're really after 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 streamreader when you're done (see using statement , idisposable] not build string lines file instead add together each line of file unique item listboxalso note if wasn't exercise arbitrary rules instead of whole code:
listboxtextooriginal.items.addrange(file.readalllines("ficheiro.txt", encoding.utf8)); c# streamreader
No comments:
Post a Comment