HomeSearch

C# StringBuilder Append and AppendLine: Add Strings

This C# example page uses the StringBuilder Append and AppendLine methods. Append can be called on its return value.

Append, AppendLine.

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

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.

And: In program 2 we combine multiple Append calls into a single statement. StringBuilder's Append() method returns a reference to itself.

C# program that uses Append syntax using System.Text; class Program { static void Main() { // Conventional StringBuilder syntax. const string s = "Value"; StringBuilder builder = new StringBuilder(); for (int i = 0; i < 1000; i++) { builder.Append("One string "); builder.Append(s); builder.Append("Another string"); } } } C# program that uses alternative Append syntax using System.Text; class Program { static void Main() { // This is a fluent interface for StringBuilder. const string s = "Value"; StringBuilder builder = new StringBuilder(); for (int i = 0; i < 1000; i++) { builder.Append("One string ").Append(s).Append("Another string"); } } }

Notes, concat.

Another option when you want to Append strings together is to use the plus operator. This approach is useful in many situations when you are not looping over strings.String AppendConcat

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.

AppendFormat
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. Here Contains("\r\n") returns true.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.

CharASCII Strings

A summary.

We can chain your StringBuilder Appends in cases where we call the method multiple times. We looked at the AppendLine method and its overloads on the StringBuilder type.
Home
Dot Net Perls
© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen, info@dotnetperls.com.