是否有一种体面的方式在C#中声明一个长单行字符串,以便在编辑器中声明和/或查看字符串是不可能的?在C中声明一个looooong单行字符串#
我知道的选项有:
1:让它运行。这很糟糕,因为你的字符串在屏幕的右侧移动,让开发人员阅读消息时不得不烦人滚动和阅读。
string s = "this is my really long string. this is my really long string. this is my really long string. this is my really long string. this is my really long string. this is my really long string. this is my really long string. this is my really long string. ";
2:@ + newlines。这在代码中看起来不错,但是会在字符串中引入换行符。此外,如果你希望它在代码中看起来不错,那么你不仅会得到换行符,而且还会在字符串的每一行的开始处出现难处理的空间。
string s = @"this is my really long string. this is my long string.
this line will be indented way too much in the UI.
This line looks silly in code. All of them suffer from newlines in the UI.";
3:"" + ...
这工作正常,但超级沮丧类型。如果我需要在某处添加一行文本,我必须更新各种+的文本并移动文本。
string s = "this is my really long string. this is my long string. " +
"this will actually show up properly in the UI and looks " +
"pretty good in the editor, but is just a pain to type out " +
"and maintain";
4:string.format or string.concat
。基本上和上面一样,但没有加号。具有相同的优点和缺点。
难道真的没有办法做到这一点吗?
字符串文字的连接由C#编译器处理,因此在运行时使用声明为“”的字符串时零开销x“+”y“+”Z“+ ...' – 2009-10-21 02:52:32
啊,@ 280Z28当然是正确的!我添加了一些关于何时使用StringBuilder方法的说明(当您将变量与您的ltierals混合使用时)。 – 2009-10-21 14:49:48