C#中的换行符


Line continue character in C#

我们有一个包含非常长字符串的变量的单元测试。

问题是如何在代码中编写它,而不会出现换行或代码难以读取的问题。

在vb中有一个行继续字符,在c中有等价物吗?


C允许在多行上拆分一个字符串,该术语称为verbatim literal

1
2
3
4
5
6
7
8
string myString = @"this is a
                    test
                   to see how long my string
                   can be



                    and it can be quite long"
;

如果您正在寻找vb中的& _的替代方案,请使用+连接您的行。


字符串恒量

只需使用+操作符,将字符串分解为人类可读的行。编译器会发现字符串是常量,并在编译时将它们连接起来。请参阅此处的msdn c编程指南。

例如

1
2
3
4
const string myVeryLongString =
   "This is the opening paragraph of my long string." +
   "Which is split over multiple lines to improve code readability," +
   "but is in fact, just one long string.";

IL_0003: ldstr "This is the opening paragraph of my long string. Which is split over multiple lines to improve code readability, but is in fact, just one long string."

字符串变量

注意,当使用字符串插值将值替换到字符串中时,需要在需要进行替换的每一行之前使用$字符:

1
2
3
4
var interpolatedString =
   "This line has no substitutions." +
    $" This line uses {count} widgets, and" +
    $" {CountFoos()} foos were found.";

但是,这会导致对string.Format的多次调用以及字符串的最终连接(用***标记)的负面性能后果。

1
2
3
4
5
6
7
8
9
10
11
IL_002E:  ldstr      "This line has no substitutions."
IL_0033:  ldstr      " This line uses {0} widgets, and"
IL_0038:  ldloc.0     // count
IL_0039:  box         System.Int32
IL_003E:  call        System.String.Format ***
IL_0043:  ldstr      " {0} foos were found."
IL_0048:  ldloc.1     // CountFoos
IL_0049:  callvirt    System.Func<System.Int32>.Invoke
IL_004E:  box         System.Int32
IL_0053:  call        System.String.Format ***
IL_0058:  call        System.String.Concat ***

虽然您可以使用$@提供单个字符串并避免性能问题,但除非空白放在{}中(这看起来很奇怪,imo),否则这与neil knight的答案有相同的问题,因为它将在行细分中包含任何空白:

1
2
3
var interpolatedString = $@"When breaking up strings with `@` it introduces
    <- [newLine and whitespace here!] each time I break the string.
    <- [More whitespace] {CountFoos()} foos were found."
;

注入的空白很容易被发现:

1
2
3
IL_002E:  ldstr      "When breaking up strings with `@` it introduces
    <- [newLine and whitespace here!] each time I break the string.
    <- [More whitespace] {0} foos were found."

另一种选择是恢复到string.Format。这里,根据我的初始答案,格式化字符串是一个常量:

1
2
3
4
const string longFormatString =
   "This is the opening paragraph of my long string with {0} chars." +
   "Which is split over multiple lines to improve code readability," +
   "but is in fact, just one long string with {1} widgets.";

然后评估如下:

1
string.Format(longFormatString, longFormatString.Length, CountWidgets());

但是,考虑到格式化字符串和替换标记之间可能存在分离,这仍然很难维护。


1
2
@"string here
that is long you mean"

但是要小心,因为

1
2
3
@"string here
           and space before this text
     means the space is also a part of the string"

它也会避开字符串中的内容

1
2
3
@"c:\\folder" // c:\\folder
@"c:\folder" // c:\folder
"c:\\folder" // c:\folder

相关的

  • 在C中,变量名前面的@符号是什么意思?
  • msdn字符串引用

您可以使用逐字文字:

1
2
3
4
const string test = @"Test
123
456
"
;

但第1行的凹痕很难看。


您必须使用以下方法之一:

1
2
3
4
5
    string s = @"loooooooooooooooooooooooong loooooong
                  long long long"
;

string s ="loooooooooong loooong" +
          " long long" ;


如果声明了不同的变量,则使用以下简单方法:

1
2
3
4
5
6
7
8
Int salary=2000;

String abc="I Love Pakistan";

Double pi=3.14;

Console.Writeline=salary+"/n"+abc+"/n"+pi;
Console.readkey();