ElektroStudios
ElektroStudios

Reputation: 20464

Multiline string variable

In .Net (C# and VB.NET) If i have a multiline text like this:

__   __                 _                       
\ \ / /                | |                      
 \ V /___  _   _ _ __  | |     ___   __ _  ___  
  \ // _ \| | | | '__| | |    / _ \ / _` |/ _ \ 
  | | (_) | |_| | |    | |___| (_) | (_| | (_) |
  \_/\___/ \__,_|_|    \_____/\___/ \__, |\___/ 
                                     __/ |      
                                    |___/       

Can I set the variable like this?

Dim Logo As String = ("
__   __                 _                       
\ \ / /                | |                      
 \ V /___  _   _ _ __  | |     ___   __ _  ___  
  \ // _ \| | | | '__| | |    / _ \ / _` |/ _ \ 
  | | (_) | |_| | |    | |___| (_) | (_| | (_) |
  \_/\___/ \__,_|_|    \_____/\___/ \__, |\___/ 
                                     __/ |      
                                    |___/       ")

Console.WriteLine(Logo)

... instead of this else:

    Console.WriteLine("__   __                                         ")
    Console.WriteLine("\ \ / /                | |                      ")
    Console.WriteLine(" \ V /___  _   _ _ __  | |     ___   __ _  ___  ")
    Console.WriteLine("  \ // _ \| | | | '__| | |    / _ \ / _` |/ _ \ ")
    Console.WriteLine("  | | (_) | |_| | |    | |___| (_) | (_| | (_) |")
    Console.WriteLine("  \_/\___/ \__,_|_|    \_____/\___/ \__, |\___/ ")
    Console.WriteLine("                                     __/ |      ")
    Console.WriteLine("                                    |___/       ")

... or this else :

            Dim Logo As String = ( _
"__   __                 _                       " & vbNewLine & _
"\ \ / /                | |                      " & vbNewLine & _
" \ V /___  _   _ _ __  | |     ___   __ _  ___  " & vbNewLine & _
"  \ // _ \| | | | '__| | |    / _ \ / _` |/ _ \ " & vbNewLine & _
"  | | (_) | |_| | |    | |___| (_) | (_| | (_) |" & vbNewLine & _
"  \_/\___/ \__,_|_|    \_____/\___/ \__, |\___/ " & vbNewLine & _
"                                     __/ |      " & vbNewLine & _
"                                    |___/       ")

Upvotes: 36

Views: 59658

Answers (6)

Guru Stron
Guru Stron

Reputation: 141755

C# 11 and .NET 7 version with raw string literals:

string multilineLogo = """
__   __                 _                       
\ \ / /                | |                      
 \ V /___  _   _ _ __  | |     ___   __ _  ___  
  \ // _ \| | | | '__| | |    / _ \ / _` |/ _ \ 
  | | (_) | |_| | |    | |___| (_) | (_| | (_) |
  \_/\___/ \__,_|_|    \_____/\___/ \__, |\___/ 
                                     __/ |      
                                    |___/       
""";
Dim MultilineLogo = """
__   __                 _                       
\ \ / /                | |                      
 \ V /___  _   _ _ __  | |     ___   __ _  ___  
  \ // _ \| | | | '__| | |    / _ \ / _` |/ _ \ 
  | | (_) | |_| | |    | |___| (_) | (_| | (_) |
  \_/\___/ \__,_|_|    \_____/\___/ \__, |\___/ 
                                     __/ |      
                                    |___/       
""";

Upvotes: 2

Geoff
Geoff

Reputation: 8850

You (initially) marked this as c#, but show VB code. For c#: use the @ specifier:

string myText =
@"line 1
line 2
line 3"

Note that if you don't want a blank line at the start of your string, make sure you put the @" on the same line as the first line of your text, as I've done above.

For VB.NET, there is no direct support for this, but you can use the nice hack from this answer to get around it:

Dim s As String = <a>line 1
line 2
line 3</a>.Value

Also consider creating a string resource; you can add line breaks in there (ensure you use shift-enter, per the note in this answer), then load the resource using something similar to

Dim myString As String = My.Resources.MyString

Update for Visual Studio 2015: Obviously vb.net was the difficult case here, but as of VS2015 it supports multi-line strings in a fashion similar to c# verbatim strings, but without the preceding @.

Note that the line terminators embedded in the string are the actual line terminators provided by your editor of choice. For VS this is \r\n.

Example:

enter image description here

Source here.

For the new interpolated strings introduced in VS2015 / C# 6, prefix the string with $@ in C#:

string multiline = $@"
[configuration]
name=Fred
age={age}";

In VB.NET, just leave out the @:

Dim multiline As String = $"
[configuration]
name=Fred
age={age}"

Upvotes: 66

Sam Leach
Sam Leach

Reputation: 12956

Use Verbatim strings.

The @ symbol tells the string constructor to ignore line breaks.

See MSDN for more information. String literals vs Verbatim strings

For example

string verbatim = @"v
                    e
                    r
                    batim"

Your example

Dim Logo As String = (@"
__   __                 _                       
\ \ / /                | |                      
 \ V /___  _   _ _ __  | |     ___   __ _  ___  
  \ // _ \| | | | '__| | |    / _ \ / _` |/ _ \ 
  | | (_) | |_| | |    | |___| (_) | (_| | (_) |
  \_/\___/ \__,_|_|    \_____/\___/ \__, |\___/ 
                                     __/ |      
                                    |___/       ")

Console.WriteLine(Logo)

Upvotes: 14

Captain Skyhawk
Captain Skyhawk

Reputation: 3500

Kind of...

It's done like this:

Dim logo = " " & vbCrLf & _
"__   __                                         " & vbCrLf & _
"\ \ / /                | |                      " & vbCrLf & _

etc.

Upvotes: 2

Vladimir
Vladimir

Reputation: 7475

In C# you can use raw strings (@), like that:

        private string Logo = @"
__   __                 _                       
\ \ / /                | |                      
 \ V /___  _   _ _ __  | |     ___   __ _  ___  
  \ // _ \| | | | '__| | |    / _ \ / _` |/ _ \ 
  | | (_) | |_| | |    | |___| (_) | (_| | (_) |
  \_/\___/ \__,_|_|    \_____/\___/ \__, |\___/ 
                                     __/ |      
                                    |___/       ";

Upvotes: 17

martavoi
martavoi

Reputation: 7082

Yes, you should use @ symbol:

string t = @"t
e
s
t"

Upvotes: 4

Related Questions