Best syntax to create multi-line string

10,759

Solution 1

What about this:

var result = string.Join(Environment.NewLine, new string[]{ 
    "one",
    "two" 
});

It's a bit painful and possibly an overkill, but it gives the possibility to preserve the lines separation in your code.
To improve things a little, you could use an helper method:

static string MultiLine(params string[] args) {
    return string.Join(Environment.NewLine, args);
}

static void Main(string[] args) {
    var result = MultiLine( 
        "one",
        "two" 
    );
}

Solution 2

What about this?

var result = "one\ntwo";

If you're fussy about OS-specific line endings, use Format:

var result = String.Format("one{0}two", Environment.NewLine);

(Well, “fussy” isn’t the right word: when dealing with text files, OS-specific line endings are often desired or even necessary, when dealing with legacy software.)

Solution 3

"Best" is a very very open ended point.

Are you after :

  • Performance of the code
  • Speed the coder can write it
  • Ability for another coder to understand it easily
  • Ability for another coder to modify it easily

All of those make a big difference as to the "best" way of doing something.

Solution 4

I'd say it depends on what You need...

But to simplify it I would go with:

var s = new StringBuilder();
s.Append("one");
s.Append("two");
s.ToString();

But since we don't know what You need it for. It's pretty difficult to give better hints

Solution 5

You should not define large strings in your source code. You should define it in an external text file:

string s = File.OpenText("myfile.txt").ReadToEnd();
Share:
10,759
Konstantin Spirin
Author by

Konstantin Spirin

Passionate software developer

Updated on June 25, 2022

Comments

  • Konstantin Spirin
    Konstantin Spirin almost 2 years

    What's the best way to create multi-line string in C#?

    I know the following methods:

    Using StringBuilder

    var result = new StringBuilder().AppendLine("one").AppenLine("two").ToString()
    

    looks too verbose.

    Using @

          var result = @"one
    two"
    

    looks ugly and badly formatted.

    Do you know better ways?

  • Konstantin Spirin
    Konstantin Spirin over 14 years
    So how do you create string "one<newline>two"?
  • Konstantin Spirin
    Konstantin Spirin over 14 years
    Not too bad. I think I'll create extension method and use like this: new[] { "One", "Two" }.Join(Environment.NewLine) or even create a class and use like this: StringHelper.JoinLines("one", "two")
  • Konstantin Spirin
    Konstantin Spirin over 14 years
    Nice but I'd prefer to separate lines with <", "> rather than with <{0}>. One character more but {0} pisses me off a bit.
  • Paolo Tedesco
    Paolo Tedesco over 14 years
    I had thought about the extension method, but in the end I think I prefer the "plain" old-style utility...
  • G Berdal
    G Berdal over 14 years
    If you need just one line I'd use Konrad's solution because it seems to me the simplest one. However, if you needed more than one line I'd use StringBuilder and Append("one" + NewLines(3) + "two") for instance.
  • Konstantin Spirin
    Konstantin Spirin over 14 years
    +1 - I always do this for relatively big files but it's not very convenient when you need to write many small tests. It also decreases locality of code.
  • Konstantin Spirin
    Konstantin Spirin over 14 years
    I did exactly this before. StringBuilder supports fluent interface - see my question why I don't like this solution.
  • Konstantin Spirin
    Konstantin Spirin over 14 years
    You answer is very similar to Konrad Rudolph's. I'd prefer to use Environment.NewLine.