35

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 & _
"                                    |___/       ")
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417

6 Answers6

63

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}"
Community
  • 1
  • 1
Geoff
  • 8,551
  • 1
  • 43
  • 50
  • 1
    Depending on the characters in your data you may also want to place an inner XML CDATA Element to prevent Encoding Issues with a couple characters. Eg. <![CDATA[your multiline data]]>.Value – DarrenMB Apr 19 '13 at 15:32
  • If you want to use multi-line string literals and string interpolation as well in C# 6/VS 2015 you need to use $@ at the beginning of the string (as opposed to @$). – Thomas Denney Jul 24 '15 at 08:16
  • in VS2015 for VB.NET atleast; you can just open a string with a quote and type, no need for anything before, just type the content like you would normally as if declaring a single line variable, but press return to any new lines. I used to append '" & _' previously to do this, this new supported way is a lot nicer as indenting is so much easier! :-) – Liam Wheldon Mar 23 '16 at 17:01
17

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

        private string Logo = @"
__   __                 _                       
\ \ / /                | |                      
 \ V /___  _   _ _ __  | |     ___   __ _  ___  
  \ // _ \| | | | '__| | |    / _ \ / _` |/ _ \ 
  | | (_) | |_| | |    | |___| (_) | (_| | (_) |
  \_/\___/ \__,_|_|    \_____/\___/ \__, |\___/ 
                                     __/ |      
                                    |___/       ";
Vladimir
  • 7,345
  • 4
  • 34
  • 39
14

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)
Sam Leach
  • 12,746
  • 9
  • 45
  • 73
4

Yes, you should use @ symbol:

string t = @"t
e
s
t"
Dzmitry Martavoi
  • 6,867
  • 6
  • 38
  • 59
2

Kind of...

It's done like this:

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

etc.

Captain Skyhawk
  • 3,499
  • 2
  • 25
  • 39
1

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

string multilineLogo = """
__   __                 _                       
\ \ / /                | |                      
 \ V /___  _   _ _ __  | |     ___   __ _  ___  
  \ // _ \| | | | '__| | |    / _ \ / _` |/ _ \ 
  | | (_) | |_| | |    | |___| (_) | (_| | (_) |
  \_/\___/ \__,_|_|    \_____/\___/ \__, |\___/ 
                                     __/ |      
                                    |___/       
""";
Dim MultilineLogo = """
__   __                 _                       
\ \ / /                | |                      
 \ V /___  _   _ _ __  | |     ___   __ _  ___  
  \ // _ \| | | | '__| | |    / _ \ / _` |/ _ \ 
  | | (_) | |_| | |    | |___| (_) | (_| | (_) |
  \_/\___/ \__,_|_|    \_____/\___/ \__, |\___/ 
                                     __/ |      
                                    |___/       
""";
Guru Stron
  • 102,774
  • 10
  • 95
  • 132