Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert LineFeed instead of CRLF

Tags:

c#

Using StringBuilder and in my string I am using Environment.NewLine, when I open it it shows as CRLF, Is there another commands in C# that the output shows as "LF" only and not "CRLF"?

like image 439
Bohn Avatar asked Aug 14 '12 14:08

Bohn


People also ask

Should I use new line or linefeed?

LF (character : \n, Unicode : U+000A, ASCII : 10, hex : 0x0a): This is simply the '\n' character which we all know from our early programming days. This character is commonly known as the 'Line Feed' or 'Newline Character'.

What is a linefeed character?

The Line Feed (LF) character moves the cursor down to the next line without returning to the beginning of the line. This character is used as the new line character in Unix based systems (Linux, macOS X, Android, etc). Codes Display.

What is carriage return and line feed in C #?

In most C compilers, including ours, the newline escape sequence '\n' yields an ASCII line feed character. The C escape sequence for a carriage return is '\r'.

How do I create a new line in C#?

By using: \n – It prints new line. By using: \x0A or \xA (ASCII literal of \n) – It prints new line. By using: Console.


2 Answers

Simply write

sb.Append((char)10);

or more readable

sb.Append('\n');

even more readable

const char LF = '\n';
sb.Append(LF);
like image 186
Olivier Jacot-Descombes Avatar answered Oct 11 '22 13:10

Olivier Jacot-Descombes


The Environment.NewLine exists solely to differ between Windows-like line endings (\r\n) and Unix-style line endings (\n), so when writing text files and the like you don't have to bother which one to use (imagine you're running on Mono on Linux, then you want just \n, which the Environment. NewLine will contain as it is set by the runtime).

So when you know you always and only want a line feed character, simply put \n in your code. It won't change.

like image 24
CodeCaster Avatar answered Oct 11 '22 12:10

CodeCaster