Convert UNIX Line Breaks

  • Thread starter Thread starter David Meier
  • Start date Start date
D

David Meier

Hi,

I am new to C# and I am facing this small problem:

I start a new process using cygwin and I redirect the standard output
to a string variable. When I display the string variable in a list box
I see those squares representing UNIX line breaks. How can I convert
those to Windows style line breaks?

Thanks. Dave.
 
My UNIX and my StringBuilder are really rusty but I think your line breaks are line feed chars - yes? A simple case approach {not nested quote aware etc...} is to replace all occurrences of LF characters with the .NET constant Environment.NewLine... My syntax isn't exact here but something like:

StringBuilder fixed = old;
fixed.Replace(LF, Environment.NewLine);
fixed.ToString();
.....

--Richard
 
Write them to a temp file and run them through unix2dos or a similar tool
(or just feed the tool from STDIN).

Sure beats writing your own conversion code.
 
Actually UNIX text files contain just "\n" (LF) at the end of the line,
while MS OS'es use "\r\n" (CR+LF)

Robert


Richard said:
My UNIX and my StringBuilder are really rusty but I think your line breaks
are line feed chars - yes? A simple case approach {not nested quote aware
etc...} is to replace all occurrences of LF characters with the .NET
constant Environment.NewLine... My syntax isn't exact here but something
like:
 
Yap. This did the trick:

StringBuilder fixed = old;
fixed.Replace("\n", Environment.NewLine);
fixed.ToString();

Thanks.
 
Klaus H. Probst said:
Write them to a temp file and run them through unix2dos or a similar tool
(or just feed the tool from STDIN).

Sure beats writing your own conversion code.

I think the code required to run the conversion tool is likely to be
considerably longer than calling String.Replace, to be honest.
 
Hi Jon,

Jon Skeet said:
I think the code required to run the conversion tool is likely to be
considerably longer than calling String.Replace, to be honest.

Well true, but what if the file is huge =)
 
Klaus H. Probst said:
Well true, but what if the file is huge =)

Then do it line by line with StreamReader - in fact, as StreamReader
accepts any kind of line break, if you're happy to process a line at a
time you don't need to do anything. Otherwise, just have one
StreamReader, and one StreamWriter to write out the file with the right
line breaks, and you'll still end up with less code than setting up the
process etc, I suspect. (It'll also reduce the dependency on an extra
tool.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top