How to insert newline in string literal?

前端 未结 12 772
没有蜡笔的小新
没有蜡笔的小新 2020-12-07 09:07

In .NET I can provide both \\r or \\n string literals, but there is a way to insert something like \"new line\" special character like Enviro

相关标签:
12条回答
  • 2020-12-07 09:54

    One more way of convenient placement of Environment.NewLine in format string. The idea is to create string extension method that formats string as usual but also replaces {nl} in text with Environment.NewLine

    Usage

       " X={0} {nl} Y={1}{nl} X+Y={2}".FormatIt(1, 2, 1+2);
       gives:
        X=1
        Y=2
        X+Y=3
    

    Code

        ///<summary>
        /// Use "string".FormatIt(...) instead of string.Format("string, ...)
        /// Use {nl} in text to insert Environment.NewLine 
        ///</summary>
        ///<exception cref="ArgumentNullException">If format is null</exception>
        [StringFormatMethod("format")]
        public static string FormatIt(this string format, params object[] args)
        {
            if (format == null) throw new ArgumentNullException("format");
    
            return string.Format(format.Replace("{nl}", Environment.NewLine), args);
        }
    

    Note

    1. If you want ReSharper to highlight your parameters, add attribute to the method above

      [StringFormatMethod("format")]

    2. This implementation is obviously less efficient than just String.Format

    3. Maybe one, who interested in this question would be interested in the next question too: Named string formatting in C#

    0 讨论(0)
  • 2020-12-07 09:54

    I like more the "pythonic way"

    List<string> lines = new List<string> {
        "line1",
        "line2",
        String.Format("{0} - {1} | {2}", 
            someVar,
            othervar, 
            thirdVar
        )
    };
    
    if(foo)
        lines.Add("line3");
    
    return String.Join(Environment.NewLine, lines);
    
    0 讨论(0)
  • 2020-12-07 09:56

    If you really want the New Line string as a constant, then you can do this:

    public readonly string myVar = Environment.NewLine;
    

    The user of the readonly keyword in C# means that this variable can only be assigned to once. You can find the documentation on it here. It allows the declaration of a constant variable whose value isn't known until execution time.

    0 讨论(0)
  • 2020-12-07 09:58

    newer .net versions allow you to use $ in front of the literal which allows you to use variables inside like follows:

    var x = $"Line 1{Environment.NewLine}Line 2{Environment.NewLine}Line 3";
    
    0 讨论(0)
  • 2020-12-07 09:59

    If you are working with Web application you can try this.

    StringBuilder sb = new StringBuilder();
    sb.AppendLine("Some text with line one");
    sb.AppendLine("Some mpre text with line two");
    MyLabel.Text = sb.ToString().Replace(Environment.NewLine, "<br />")
    
    0 讨论(0)
  • 2020-12-07 10:04

    If I understand the question: Couple "\r\n" to get that new line below in a textbox. My example worked -

       string s1 = comboBox1.Text;     // s1 is the variable assigned to box 1, etc.
       string s2 = comboBox2.Text;
    
       string both = s1 + "\r\n" + s2;
       textBox1.Text = both;
    

    A typical answer could be s1 s2 in the text box using defined type style.

    0 讨论(0)
提交回复
热议问题