4

I am wondering how can you do multi line strings without the concat sign(+)

I tried this

 string a = String.Format(@"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
                                            {0}xxxxxxx", "GGG");

            string b = @"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
                                            xxxxxxx";


            string c = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
                                            + "xxxxxxx";
           Console.WriteLine(a);
           Console.WriteLine(b);
           Console.WriteLine(c);

This works but the first 2 render alot of whitespace. Is there away to write multi lines without the concat sign and ignore end whitespace without using something like replace after the string is rendered?

Edit

using System; using
System.Collections.Generic; using
System.Linq; using System.Text;

namespace ConsoleApplication2 {
    public class Test
    {

        public Test()
        {
        }

        public void MyMethod()
        {
            string someLongString = "On top of an eager overhead weds a
pressed vat.  How does a chance cage
the contract? The glance surprises the
radical wild.";
        }

} }

Look how it goes right though my formating of the braces and is sticking out(sorry kinda hard to show on stack but it is sticking way to far out).

Sad thing is notepad++ does a better job and when it wraps it goes right under the variable.

9
  • 1
    Just don't add all that extra spaces... Commented May 30, 2011 at 20:15
  • What is the use case for this? Commented May 30, 2011 at 20:15
  • I either 1) Align subsequent lines of the literal to the left. (They must be manually indented anyway, so this is actually "easier") 2) Put the string literal in a Resource file (the default string resource editor is annoying to use, however) or 3) Just use the concatenation approach. Happy coding. Commented May 30, 2011 at 20:16
  • 1
    @chobo2, you can take the high road and put those strings in resources. That also brings you localization almost for free. Commented May 30, 2011 at 20:18
  • @ RedFilter = Think if I got a very long sentence that I want to break up into multiple lines to make it easier to read in the code. Commented May 30, 2011 at 20:18

6 Answers 6

7

Just remove the white space. It may not be pretty, but it works:

string a = @"this is the first line
and this is the second";

Note that the second line needs to go all the way to the left margin:

multiline verbatim string

As an alternative, you can use the \n character, but then you can't use verbatim strings (the @-prefix):

string a = "first line\nsecond line";
Sign up to request clarification or add additional context in comments.

2 Comments

I am just trying to make long sentences more readable in code while. So I don't think the second option will work.
@chobo2: I would argue that long sentences should not be in the code in the first place, but rather in a resource file.
6

There's no form of string literal which allows multi-line strings but trims whitespace at the start of each line, no.

6 Comments

That's too bad. What is the recommend way of making a long sentence in code look readable to the coder and preserve formatting to be displayed? I find it some times a page if you got long sentence or something and you have to start using the concat sign. Find it makes it harder to read. Also like my first example what happens when you got a long sentence in a String.Format how can you break that apart so it is not one one line.
@chobo2 The easiest way is to enable Word Wrap in your editor, see my answer.
@chobo2: Usually I find if I want a large amount of text in code, it's better to include it in a separate resource within the assembly. That way I don't need to worry about escaping etc.
You can put place holders in the resource files? So if say I have along sentence like "Mr. bob, Hi How are you....."I could put "{0} {1}, Hi how are you"
@chobo2: You can put the string in the resource file, and then use string.Format as normal.
|
1

Any whitespace you include in the multiline string will always be included.

However ... try formatting your code like this:

string a = String.Format(
@"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
{0}xxxxxxx", "GGG");

... hopefully that looks better.

Comments

0

The solution is to enable Word Wrap when viewing code like this. That will both preserve the formatting and allow you to view the entire string wrapped without having to scroll in the editor.

You can do this by pressing Ctrl+e, W, or by selecting Edit/Advanced/Word Wrap from the menu.

1 Comment

Ok I thought this would be the solution to the problem but one thing that I noticed and don't like is that it screws with the formatting(see my edit)
0

You can do this while preserving code formatting, assuming the following:

  • Regex is acceptable
  • Your strings' whitespaces are always maximum 1 character long:

var words = Regex.Replace( @"lorem ipsum dolor sit amet consectetur adipiscing elit sed feugiat consectetur pellentesque nam ac elit risusac blandit dui duis rutrum porta tortor ut convallis duis rutrum porta tortor ut yth convallis lorem ipsum dolorsit amet consectetur adipiscing elit sed feugiat consectetur pellentesque nam ac elit risus ac blandit dui duis rutrum porta tortor ut convallis duis rutrum porta tortor ut convallis ", @"\s+", " ");

Comments

0

C# 11 has Raw string literal

We can use it like below

var xml = """
        <element attr="content">
            <body>
            </body>
        </element>
        """;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.