Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preformatted text in VB - What is the C# @ equivalent in vb?

Tags:

c#

.net

vb.net

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?

like image 765
Micah Avatar asked May 01 '09 13:05

Micah


People also ask

What is string format 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.


6 Answers

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 image 109
Joseph Avatar answered Oct 19 '22 23:10

Joseph


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"
like image 29
Adam Neal Avatar answered Oct 19 '22 23:10

Adam Neal


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

like image 45
Meta-Knight Avatar answered Oct 20 '22 00:10

Meta-Knight


Actually you can do this in vb.net. You use something called XML Literals.

Dim mystring = <string>this is
 some preformatted
 text</string>.Value
like image 30
devSpeed Avatar answered Oct 19 '22 23:10

devSpeed


You might want to try Alex Papadimoulis' "Smart Paster" add-in. It lets you paste a string into C# or VB code "as StringBuilder".

like image 22
Brian Schroer Avatar answered Oct 19 '22 23:10

Brian Schroer


VB is weak for string manipulation. No pre-formatting or inline escape characters. Any speacial characters need to be appended to the string.

like image 28
stevehipwell Avatar answered Oct 20 '22 00:10

stevehipwell