This is probably really obvious and I'm being dense. In C# I can do this:
string = @"this is
some preformatted
text";
How do I do this in VB?
In visual basic, the string Format method is useful to insert the value of variable or an object or expression into another string. By using the string Format method, we can replace the format items in the specified string with the string representation of specified objects.
There isn't one.
In C# you have the ability to do something like this "This ends in a new line\n.", but in VB there's no concept of that, you have predefined variables that handle that for you like "This ends in a new line" & vbNewLine
Hence, there's no point in a string literal (@"something\n") because in VB it would be interpreted literally anyway.
The problem with VB .NET is that a statement is deemed terminated at the end of a line, so you can't do this
Dim _someString as String = "Look at me
I've wrapped my string
on multiple lines"
You're forced to terminate your string on every line and use an underscore to indicate you wish to continue your statement, which makes you do something like
Dim _someString as String = "Look at me " & vbNewLine &_
"*** add indentation here *** I've wrapped my string " & vbNewLine &_
vbTab & " on multiple lines" '<- alternate way to indent
Like others said, there's no @ operator, so if you get into heavy string manipulation, use String.Format
IMHO, this
Dim text As String = String.Format("this is {0} some preformatted {0} text", Environment.Newline)
is more readable than this
Dim text As String = "this is" & Environment.NewLine _
& " some preformatted" & Environment.NewLine _
& " text"
I don't think you can do this in VB. You have to do:
Dim text As String = "this is" & Environment.NewLine _
& " some preformatted" & Environment.NewLine _
& " text"
Edit: As suggested in comments, replaced VB specific vbNewLine by Environment.NewLine
Actually you can do this in vb.net. You use something called XML Literals.
Dim mystring = <string>this is
some preformatted
text</string>.Value
You might want to try Alex Papadimoulis' "Smart Paster" add-in. It lets you paste a string into C# or VB code "as StringBuilder".
VB is weak for string manipulation. No pre-formatting or inline escape characters. Any speacial characters need to be appended to the string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With