How to insert new line, other characters in a string?

  • Thread starter Thread starter deko
  • Start date Start date
D

deko

In VB, I would do this for a new line:

str = "text here" & VbCrLf & "more text"

For other characters, I would use the ascii code:

For example, Chr(34) is a double quote (").

how do I do this kind of stuff in C#?

Can anyone point me to a reference?

Thanks!
 
CrLf stands for Control Return and Line Feed... two characters that you can
individually specify with a couple of escape characters, namely \r and \n
respectively, so for your example you would use:

str = "text here" + "\r\n" + "more text"

Brendan
 
There are 11 predefined literals:

\’ single quote
\" double quote
\\ backslash
\0 null character
\a alert
\b backspace
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab

For others, use the Unicode value '\uXX'

Mark
 
The .NET equivelant to vbCrLf is System.Environment.NewLine

So...

string str = "line one" + System.Environment.NewLine + "line two"

or ...

string str = System.String.Format("line 1{0}line 2",
System.Environment.NewLine)
 
KH said:
The .NET equivelant to vbCrLf is System.Environment.NewLine

So...

string str = "line one" + System.Environment.NewLine + "line two"

Well, that's the equivalent in .NET, but it's not guaranteed to be the
equivalent on all CLI implementations.

vbCrLf is (as the name suggests) carriage return / line feed - "\r\n".

Environment.NewLine is the normal new line sequence *for the
environment of the CLI*. On Linux, for instance, that's probably \n.

Sometimes you should use the normal environment version, other times
(particularly when following standards) you should use precise values
which won't be environmentally dependent.
 
There are 11 predefined literals:
\' single quote
\" double quote
\\ backslash
\0 null character
\a alert
\b backspace
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab

For others, use the Unicode value '\uXX'

Great! Armed with this and the reference sheet at
http://www.unicode.org/charts/ I'm off to the races...

And I just learned that my alphabet is "Basic Latin".... you mean there are
others? ;)
 
Brendan Grant said:
CrLf stands for Control Return and Line Feed... two characters that you
can
individually specify with a couple of escape characters, namely \r and \n
respectively, so for your example you would use:

str = "text here" + "\r\n" + "more text"

Brendan

And instead of concatenating the strings, make it one string (Although, with
literals, the compiler will do it for you):

str = "text here\r\nmore text";

:)

Mythran
 

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