C# StringBuilder Append and AppendLine

Add strings with the StringBuilder Append and AppendLine methods.

dot net perls

Append, AppendLine. Append adds data to a StringBuilder. Append method calls can be chained. This makes StringBuilder code more succinct and easier to read.StringBuilder

With AppendLine, the Framework places a newline sequence at the end of the specified string. This method makes code easier to read as well.

An example. Remember that StringBuilder often improves your application's performance. But excessive method calls, on separate lines, can be hard to read.
Version 1: In this version of the code we call Append() on the reference to the StringBuilder repeatedly.
Version 2: Here we combine multiple Append calls into a single statement. Append() returns a reference to the StringBuilder.
C# program that uses Append syntax using System; using System.Text; class Program { static void Main() { const string s = "(s)"; // Version 1: call Append repeatedly. StringBuilder builder = new StringBuilder(); for (int i = 0; i < 3; i++) { builder.Append("Start"); builder.Append(s); builder.Append("End"); } // Version 2: use fluent interface for StringBuilder. StringBuilder builder2 = new StringBuilder(); for (int i = 0; i < 3; i++) { builder2.Append("Start").Append( s).Append("End"); } Console.WriteLine("BUILDER: {0}", builder); Console.WriteLine("BUILDER 2: {0}", builder2); } } Output BUILDER: Start(s)EndStart(s)EndStart(s)End BUILDER 2: Start(s)EndStart(s)EndStart(s)End

AppendLine example. AppendLine adds strings with a newline. With AppendLine, a newline sequence is automatically inserted after the argument is added to the StringBuilder buffer.
Arguments: AppendLine is called with 0 or 1 arguments. When you call AppendLine with no arguments, it just appends the newline sequence.
String: With a string argument, it appends that string and the newline sequence. There is no way to use a format string with AppendLine.
C# program that uses AppendLine using System; using System.Text; class Program { static void Main() { // Use AppendLine. StringBuilder builder = new StringBuilder(); builder.AppendLine("One"); builder.AppendLine(); builder.AppendLine("Two").AppendLine("Three"); // Display. Console.Write(builder); // AppendLine uses \r\n sequences. Console.WriteLine(builder.ToString().Contains("\r\n")); } } Output One Two Three True

Note, newlines. The newline sequence used in AppendLine is equal to "\r\n" which is also available by referencing Environment.NewLine.Environment.NewLineContains

Discussion. There are 2 ways to specify newlines: the character "\n" alone, and the sequence "\r\n". AppendLine always uses the sequence "\r\n". To save bytes, you can manually use "\n".
Tip: This means every line in your text output will be one character shorter. In ASCII format, this will save one byte per line.
And: Your C# program will save 2 bytes per line in its memory, because a character is 2 bytes long.

A summary. We can chain StringBuilder Appends in cases where we call the method multiple times. We looked at the AppendLine method and its overloads on the StringBuilder type.


© 2007-2020 sam allen. send bug reports to info@dotnetperls.com.