C#: New line and tab characters in strings

前端 未结 6 1426
名媛妹妹
名媛妹妹 2021-01-17 07:40
StringBuilder sb = new StringBuilder();
sb.Append(\"Line 1\");
//insert new line character
//insert tab character
sb.Append(\"Line 2\");
using (StreamWriter sw = new         


        
相关标签:
6条回答
  • 2021-01-17 07:54

    Use:

    sb.AppendLine();
    sb.Append("\t");
    

    for better portability. Environment.NewLine may not necessarily be \n; Windows uses \r\n, for example.

    0 讨论(0)
  • 2021-01-17 07:58
    sb.AppendLine();
    

    or

    sb.Append( "\n" );
    

    And

    sb.Append( "\t" );
    
    0 讨论(0)
  • 2021-01-17 08:05

    It depends on if you mean '\n' (linefeed) or '\r\n' (carriage return + linefeed). The former is not the Windows default and will not show properly in some text editors (like Notepad).

    You can do

    sb.Append(Environment.NewLine);
    sb.Append("\t");
    

    or

    sb.Append("\r\n\t");
    
    0 讨论(0)
  • 2021-01-17 08:10
    sb.Append(Environment.Newline);
    sb.Append("\t");
    
    0 讨论(0)
  • 2021-01-17 08:15
        StringBuilder SqlScript = new StringBuilder();
    
        foreach (var file in lstScripts)
        {
            var input = File.ReadAllText(file.FilePath);
            SqlScript.AppendFormat(input, Environment.NewLine);
        }
    

    http://afzal-gujrat.blogspot.com/

    0 讨论(0)
  • 2021-01-17 08:16
    StringBuilder sb = new StringBuilder();
    sb.Append("Line 1");
    sb.Append(System.Environment.NewLine); //Change line
    sb.Append("\t"); //Add tabulation
    sb.Append("Line 2");
    using (StreamWriter sw = new StreamWriter("example.txt"))
    {
        sw.Write(sb.ToString());
    }
    

    You can find detailed documentation on TAB (and other escape character here).

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