How to define C# multiline string literal?


I would like to know how we can define multiline string literal in C#, in an easy way? For exmaple, If I have to do like this

string str= "Hello World"
+ " From India"
+ " by Rohan";

It makes difficult when we have lots of lines of string.


Asked by:- Vinnu
0
: 179 At:- 10/14/2022 3:35:11 PM
C# c# multiline string







1 Answers
profileImage Answered by:- vikas_jk

Multiline string liertal in C#, can be created using '@' (verbatim identifier), here is an example

		string str= @"Hello World
					From India
					by Rohan";
		Console.WriteLine(str);

Output:

Hello World
From India
by Rohan

You can also add values in the middle of multline string literal in C#, using $@ in C# 6 or above, as shown below

            string text = "Customer";
            string query = $@"SELECT *
		FROM {text}Table
		WHERE id = 007";
            Console.WriteLine(query);

You may also refer to this article: Multiple ways to add newline into a String in C#

C# String Interpolation

That's it, hope it helps.

1
At:- 10/14/2022 3:47:07 PM
thanks for the details 0
By : Vinnu - at :- 10/14/2022 3:50:32 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use

Subscribe Now

Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly