Best way to break long strings

Verbatim String Literal

A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character.

Multiline String Literals

Multiline String in C# asp.net

Have you ever been in a situation where breaking a long string in multiple lines ? It's called a Multiline String Literals, and it's just a matter of putting @ before the literal. Not only does this allow multiple lines, but it also turns off escaping.

The following example demonstrates how to construct a multiline string literal.

Method 1:
string line1 = @"First Line
    Second Line
    Third Line
    Forth Line";
Method 2:
string line1 = "First Line \n" +
    "Second Line \n" +
    "Third Line \n" +
    "Forth Line";
Method 3:
var someString = String.Join(
	Environment.NewLine,
	"First Line",
	"Second Line",
	"Third Line",
	"Forth Line");
Continue Long Statements on Multiple Lines asp.net C# vb.net

VB.Net

Method 1:

In VB.Net you can use XML Literals to achieve a similar effect:

Imports System.Xml
Imports System.Xml.Linq
Dim str As String = First Line
                    Second Line
                    Third Line
                    Forth Line.Value
Method 2:
Dim str As String = "First Line" & vbCrLf &
        "Second Line" & vbCrLf &
        "Third Line" & vbCrLf &
        "Forth Line"
Method 3:

If you have any special characters included in the string , you should use a CDATA block.

Dim str As String = < ![CDATA[First Line
< Second Line >
Third Line &
Forth Line]]  > .Value
How to escape double quotes asp.net C# vb.net

Escape double quotes in string

A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. Not only does this allow multiple lines, but it also turns off escaping.

C#
string quote = @"Before Quotes, ""Inside Quotes,"" - After that!";
VB.Net

In VB.Net you put the quotes within another double quote.

Dim str As String = "Before Quotes, ""Inside Quotes,"" - After that!"


NEXT.....Encryption and Decryption