Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 & _ "                                    |___/       ") 
like image 984
ElektroStudios Avatar asked Apr 19 '13 15:04

ElektroStudios


People also ask

How do you type a multiline string?

Use triple quotes to create a multiline string It is the simplest method to let a long string split into different lines. You will need to enclose it with a pair of Triple quotes, one at the start and second in the end. Anything inside the enclosing Triple quotes will become part of one multiline string.

How do I store a multiline string to a variable in Bash?

Bash Escape Characters For example, if we have a multiline string in a script, we can use the \n character to create a new line where necessary. Executing the above script prints the strings in a new line where the \n character exists.

How do you make a string variable with multiple lines in Python?

The escape sequence — backslash ('\') is used to create multiline strings in Python. While creating multiline strings using backslash(\), the user needs to explicitly mention the spaces between the strings.

What is the use of multiline string?

Summary: Python Multiline String gives better readability. Three single quotes, three double quotes, brackets, and backslash can be used to create multiline strings. Whereas the user needs to mention the use of spaces between the strings.


2 Answers

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}" 
like image 70
Geoff Avatar answered Oct 14 '22 03:10

Geoff


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

        private string Logo = @" __   __                 _                        \ \ / /                | |                        \ V /___  _   _ _ __  | |     ___   __ _  ___     \ // _ \| | | | '__| | |    / _ \ / _` |/ _ \    | | (_) | |_| | |    | |___| (_) | (_| | (_) |   \_/\___/ \__,_|_|    \_____/\___/ \__, |\___/                                       __/ |                                           |___/       "; 
like image 35
Vladimir Avatar answered Oct 14 '22 03:10

Vladimir