代码之家  ›  专栏  ›  技术社区  ›  Shiraz Bhaiji

C中的换行符#

c#
  •  58
  • Shiraz Bhaiji  · 技术社区  · 15 年前

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

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

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

    6 回复  |  直到 9 年前
        1
  •  54
  •   Neil Knight    13 年前

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

    string myString = @"this is a
                        test
                       to see how long my string
                       can be
    
    
    
                        and it can be quite long";
    

    如果你正在寻找替代 & _ 从vb,使用 + 加入你的行列。

        2
  •  47
  •   Community Mohan Dere    9 年前

    字符串恒量

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

    例如

    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."

    字符串变量

    注意,当使用字符串插值将值替换为字符串时, $ 字符必须在前面 每个 需要替换的行:

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

    然而,这有 负业绩 多次调用的结果 string.Format 以及字符串的最终连接(标记为 *** )

    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 ***
    

    尽管你可以使用 $@ 为了提供单个字符串并避免性能问题, unless the whitespace is placed inside {} (这看起来很奇怪,IMO),这和尼尔·奈特的答案有着相同的问题,因为它将包括任何行分解中的空白:

    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.";
    

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

    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."
    

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

    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.";
    

    然后评估如下:

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

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

        3
  •  6
  •   Community Mohan Dere    9 年前
    @"string here
    that is long you mean"
    

    但是要小心,因为

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

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

    @"c:\\folder" // c:\\folder
    @"c:\folder" // c:\folder
    "c:\\folder" // c:\folder
    

    相关的

        4
  •  5
  •   Henk Holterman    15 年前

    您可以使用逐字文字:

    const string test = @"Test
    123
    456
    ";
    

    但第1行的凹痕很难看。

        5
  •  0
  •   Liviu Mandras    15 年前

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

        string s = @"loooooooooooooooooooooooong loooooong
                      long long long";
    
    string s = "loooooooooong loooong" +
               " long long" ;
    
        6
  •  -1
  •   slavoo user3099232    10 年前

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

    Int salary=2000;
    
    String abc="I Love Pakistan";
    
    Double pi=3.14;
    
    Console.Writeline=salary+"/n"+abc+"/n"+pi;
    Console.readkey();