Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove LF while StreamReader.ReadLine

Status
Not open for further replies.

ralphtrent

Programmer
Jun 2, 2003
958
US
Hello
I have a line break in the middle of a line. Notepad shows it as one line however. It is not a Carriage Return, just a line feed. When I attempt to read the line using StreamReader.ReadLine(), it reads up to the LF, then stops, when I ReadLine again, it is the continuation. I have tried replace \n and \r to not avail. I think that is because StreamReader already sees the new line before I replace it.

here is my code

Code:
while (!lsrReadInput1.EndOfStream)
{
     string lstrCurrentLine = lsrReadInput1.ReadLine().Replace('\n', ' ');
[green] attempt to do stuff with lstrCurrentLine[/green]
}
[code]

Any help is appreciated.
 
You could do this:

Code:
while (!lsrReadInput1.EndOfStream)
{
    string lstrCurrentLine = lsrReadInput1.ReadLine();

    if (lstrCurrentLine.EndsWith('\n'))
        lstrCurrentLine += lsrReadInput1.ReadLine();

    // attempt to do stuff with lstrCurrentLine
}

Or better still put the [if] statement in a loop in case of multiple line feeds.
 
Thats an interesting idea. I will have to try that.
Thanks.
 
[0] I wonder EndsWith()'s return is of any help here. As op said, ReadLine() has already made up its mind, encounting the end of a line, the string's EndsWith() won't know what the line terminator is.

[1] Actually, the line terminator of ReadLine() method is defined in the documentation.
quote
[tt]A line is defined as a sequence of characters followed by a line feed ("\n"), a carriage return ("\r") or a carriage return immediately followed by a line feed ("\r\n"). The string that is returned does not contain the terminating carriage return or line feed. The returned value is nullNothingnullptra null reference (Nothing in Visual Basic) if the end of the input stream is reached.[/tt]
unquote

[2] One way to by-passing the problem is to replace the standalone "\n" by some signature string, say, "[LF]", as an example hereinbelow, or something else of easy recognition.
[tt]
[green]//using System.Text.RegularExpressions;[/green]
[blue]string LF="[LF]";[/blue]
string u=lsrReadInput1.ReadToEnd();
string v=Regex.Replace(u,@"(?<!\r)\n",LF);
string[] astr=Regex.Split(v,Environment.NewLine);
[blue]//process the astr entry by entry... as lstrCurrentLine[/blue]
[/tt]
 
my concern with doing something like
Code:
string u = lsrReadlInput1.ReadToEnd();
is that hte file can be big and storing all that into a variable my have undesireable results no?

Thanks for all the help.
 
undesirable" is only the bigger memory footprint, nothing of the sort like corruption of data being "undesirable". Price to pay for "dirty" data, one way or another?! Lowest memory footprint is to read character by character until you encounter \r\n... effective making your own parser. In terms of expensive string manipulation, that's more inefficient in processing time, I would say. (Regex gains more edge for large string.) Otherwise, tackle the problem at the origin (the production level) of it. It is to your judgement.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top