我正在使用Visual Studio 2010 C#创建一个应用程序。
我的代码中有一个字符串,导致代码无法编译。该字符串包含换行符,因此跨越多行,而不是在一行上。
我该怎么办才能保持字符串换行符,但也要编译代码?
答案 0 :(得分:7)
我认为您需要在字符串前使用@
符号,以允许将其视为字符串文字并跨越多行。
string myString = @"blah blah
blah blah
blah blah";
如果你的字符串包含"
字符,则需要加倍,因为我认为转义不会在字符串文字中起作用:
string myString = @"blah ""blah""
blah blah
""blah"" blah";
详细了解规范here
中的字符串文字答案 1 :(得分:2)
您可以简单地连接字符串。所以
string s = "I am a very long string that will probably not adhere to your coding " +
"guidelines. So I am split across several concateneted strings.";
s += "Since this might look silly the += operator is used, too.";
将所有行放入字符串s。
这会回答你的问题吗?