简体   繁体   English

创建多行字符串的最佳语法

[英]Best syntax to create multi-line string

What's the best way to create multi-line string in C#? 在C#中创建多行字符串的最佳方法是什么?

I know the following methods: 我知道以下方法:

Using StringBuilder 使用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? 你知道更好的方法吗?

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" 
    );
}

What about this? 那这个呢?

var result = "one\ntwo";

If you're fussy about OS-specific line endings, use Format : 如果您对特定于操作系统的行尾感到困惑,请使用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.) (好吧,“ fussy”不是正确的词:在处理文本文件时,在处理旧版软件时,通常需要甚至什至有必要使用特定于操作系统的行尾。)

"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. 所有这些对于做某事的“最佳”方式都有很大的不同。

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 很难给出更好的提示

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();

Riffing off of what @codymanix said , you could place the long multiline string in a resource file. 摆脱@codymanix所说的 ,您可以将长的多行字符串放在资源文件中。 This can be easier for certain deployment scenarios since the text "file" will be included in your DLL / EXE. 对于某些部署方案,这可能会更容易,因为文本“文件”将包含在DLL / EXE中。

Ehm, how about: 嗯,怎么样:

string s = 
  "abc\n" + 
  "def\n" ;

Sometimes you need more than one line. 有时您需要多个线路。 I use Environment.NewLine but I placed it in a method to multiply it. 我使用Environment.NewLine,但我将其放置在一个方法中以使其倍增。 :) :)

    private string newLines(int multiplier)
    {
        StringBuilder newlines = new StringBuilder();
        for (int i = 0; i < multiplier; i++)
            newlines.Append(Environment.NewLine);
        return newlines.ToString();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM