tags:

views:

162

answers:

4
+1  Q: 

New line in VB.NET

Why, when I do im my code:

"Land Location \\r\\n Roundoff (C)"

I see the \\r\\n and not a new line feeder at the output?

Any idea how to do that?

As I said I must have only one string there, without using a "&". Can I put that vbCrLf inside of my string somehow?

+3  A: 

May be "Land Location " & vbCR & vbLF .....

S.Mark
I must have it in the same string , and not starting adding it
Night Walker
it is 'in the same string', the compiler will concatenate constant strings
Mitch Wheat
Night Walker
You could do `String.Format("line 1{0}line 2", vbCrLf)`, but it's really still concatination
David Hedlund
why MUST you 'see' one string? what difference does that make?
Mitch Wheat
Localization that's why
Night Walker
Localization has nothing to do with it. A correctly localized string will replace your literal with a resource variable name. That means a code change later anyway, and so it doesn't matter how much concatenation you're doing right now.
Joel Coehoorn
I am sorry but it is . If you want to put in the resources a full string and not parts of it ? So the translator will see the whole string that he has to translate and not parts of it .
Night Walker
+1  A: 
vbCrLf
vbCr
vbLf
Mitch Wheat
I must have all written as one string .
Night Walker
+5  A: 

Instead of including the newline manually in the String use System.Environment.NewLine.

monksy
+1. This is the recommended way of doing it, but 99 times out of 100 it is unlikely you will be swapping to Linux (say).
Mitch Wheat
...If we are talking VB.NET and not just VB...
Mitch Wheat
It was tagged as VB.net. Also... it helps with porting the code over to C# as well.
monksy
Never seem that way of doing a newline, good to know.
Wade73
+6  A: 

There is no \ escape codes in VB so you can't put a line break in a string literal. The only escape character in VB strings is the double quotation marks used to insert a quotation mark in a string.

You can use the VB constant for a Windows type line break:

"Land Location " & vbCrLf & " Roundoff (C)"

For the code to be platform independent, you should use the NewLine property instead:

"Land Location " & Environment.NewLine & " Roundoff (C)"

Whether you should use the platform independent code or not depends on the situation.

If you need it as a single string for some reason, you would have to use a marker for the line break, that you replace when you use the string:

Dim s As String = "Land Location \n Roundoff (C)"
s = Replace(s, "\n", Environment.NewLine)
Guffa
There is no sense in doing that. Just do a String format.
monksy
@steven: What are you talking about? Where and why would String.Format make any more sense?
Guffa
+1 for Environment.NewLine instead of vbCrLf or vbNewLine
PhilPursglove