开发者

C#: New line and tab characters in strings

开发者 https://www.devze.com 2022-12-18 22:54 出处:网络
StringBuilder sb = new StringBuilder(); sb.Append(\"Line 1\"); //insert new line character //insert tab character
StringBuilder sb = new StringBuilder();
sb.Append("Line 1");
//insert new line character
//insert tab character
sb.Append("Li开发者_如何学Gone 2");
using (StreamWriter sw = new StreamWriter("example.txt"))
{
    sq.Write(sb.ToString());
}

How can insert a new line and tab character in this example?


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


Use:

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

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


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


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


sb.AppendLine();

or

sb.Append( "\n" );

And

sb.Append( "\t" );


    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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号