C# String and StringBuilder Tutorial

1- Hierarchical Inheritance

When working with text data, CSharp provides you with three classes including  String and StringBuilder. When working with big data, you should use StringBuilder to optimize the efficiency. Basically, 2 classes have many similarities.
  • String is immutable (More details of this concept are in the document). It does not allow the existence of subclass.
  • StringBuilder is mutable.

2- Mutable and immutable concept

Consider an illustration:
// This is a class owning a Value field.
// After creating the object, you can set new value
// for Value field by calling SetNewValue(int) method.
// This is a mutable class.
class MutableClassExample
{
  private int Value;

  public MutableClassExample(int value)
  {
      this.Value = value;
  }

  public void SetNewValue(int newValue)
  {
      this.Value = newValue;
  }
}


// This is a class with Value field and Name field.
// When you create this class, you cannot reset the Value and all other fields from outside.
// This class does not have methods for the purpose of resetting fields from outside.
// It means this class is immutable
class ImmutableClassExample
{
  private int Value;
  private String Name;

  public ImmutableClassExample(String name, int value)
  {
      this.Value = value;
      this.Name = name;
  }

  public String GetName()
  {
      return Name;
  }

  public int GetValue()
  {
      return Value;
  }
}
String is a immutable class. String includes various fields like length, but values in these fields can not change

3- String and string

In C #, sometimes you see String and string are used in parallel. In fact, they do not have any difference, string can be seen as an alias  for System.String (full name including the namespace of the  String class).

The table below describes the full list of aliases of the common class.
Alias Class
object System.Object
string System.String
bool System.Boolean
byte System.Byte
sbyte System.SByte
short System.Int16
ushort System.UInt16
int System.Int32
uint System.UInt32
long System.Int64
ulong System.UInt64
float System.Single
double System.Double
decimal System.Decimal
char System.Char

4- String

String is one of the most important class in CSharp and anyone who starts with CSharp programming uses String to print something on console by using famous Console.WriteLine() statements. Many C# beginners not aware that String is immutable and sealed (Do not allow subclasses). In C# and every modification in String result creates a new String object.
[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class String : IComparable, ICloneable, IConvertible,
    IEnumerable, IComparable<string>, IEnumerable<char>, IEquatable<string>

4.1- The methods of the String

You can look at the methods of String:
Below is a list of some common methods of String.
Some String methods
public bool EndsWith(string value)

public bool EndsWith(string value, StringComparison comparisonType)

public bool Equals(string value)

public int IndexOf(char value)

public int IndexOf(char value, int startIndex)

public int IndexOf(string value, int startIndex, int count)

public int IndexOf(string value, int startIndex, StringComparison comparisonType)

public int IndexOf(string value, StringComparison comparisonType)

public string Insert(int startIndex, string value)

public int LastIndexOf(char value)

public int LastIndexOf(char value, int startIndex)

public int LastIndexOf(char value, int startIndex, int count)

public int LastIndexOf(string value)

public int LastIndexOf(string value, int startIndex)

public int LastIndexOf(string value, int startIndex, int count)

public int LastIndexOf(string value, int startIndex, int count, StringComparison comparisonType)

public int LastIndexOf(string value, int startIndex, StringComparison comparisonType)

public int LastIndexOf(string value, StringComparison comparisonType)

public int LastIndexOfAny(char[] anyOf)

public int LastIndexOfAny(char[] anyOf, int startIndex)

public int LastIndexOfAny(char[] anyOf, int startIndex, int count)

public int IndexOf(string value, int startIndex, int count, StringComparison comparisonType)

public string Replace(char oldChar, char newChar)

public string Replace(string oldValue, string newValue)

public string[] Split(params char[] separator)

public string[] Split(char[] separator, int count)

public string[] Split(char[] separator, int count, StringSplitOptions options)

public string[] Split(char[] separator, StringSplitOptions options)

public string[] Split(string[] separator, StringSplitOptions options)

public bool StartsWith(string value)

public bool StartsWith(string value, bool ignoreCase, CultureInfo culture)

public bool StartsWith(string value, StringComparison comparisonType)

public string Substring(int startIndex)

public string Substring(int startIndex, int length)

public char[] ToCharArray()

public char[] ToCharArray(int startIndex, int length)

public string ToLower()

public string ToLower(CultureInfo culture)

public string ToLowerInvariant()

public override string ToString()

public string ToUpper()

public string ToUpper(CultureInfo culture)

public string ToUpperInvariant()

public string Trim()

public string Trim(params char[] trimChars)

public string TrimEnd(params char[] trimChars)

public string TrimStart(params char[] trimChars)

 

4.1.1- Length

Length is a property of the string, it returns the number of characters (Unicode) in the this string.
LengthDemo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StringTutorial
{
   class LengthDemo
   {
       public static void Main(string[] args)
       {
           String str = "This is text";

           // Length is a property of string.
           // Get the number of characters in this string.
           int len = str.Length;

           Console.WriteLine("String Length is : " + len);

           Console.Read();
       }
   }
}
Results of running the example:

4.1.2- Concat(...)

Concat is a static method used to concatenate multiple strings together and returns a new String.
ConcatDemo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StringTutorial
{
   class ConcatDemo
   {
       public static void Main(string[] args)
       {
           string s1 = "One";
           string s2 = "Two";
           string s3 = "Three";

           // Same as s1 + s2
           string s = String.Concat(s1, s2);

           Console.WriteLine("Concat s1, s2 : " + s);

           // Same as s1 + s2 + s3
           s = String.Concat(s1, s2, s3);

           Console.WriteLine("Concat s1, s2, s3 : " + s);

           Console.Read();
       }
   }


}
Results of running the example:

4.1.3- IndexOf(..)

IndexOf(..) là một phương thức trả về chỉ số vị trí xuất hiện lần đầu tiên một ký tự chỉ định hoặc một chuỗi con chỉ định trong chuỗi hiện tại. Có 8 phương thức IndexOf nhưng khác nhau tham số. Chỉ số được tính bắt đầu từ số 0 (Không phải 1).
IndexOfDemo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StringTutorial
{
  class IndexOfDemo
  {
      public static void Main(string[] args)
      {
          String str = "This is text";

          // Find index within this string of the first occurrence 'i'.
          int idx = str.IndexOf('i'); // ==> 2
          Console.WriteLine("- IndexOf('i') = " + idx);


          // Find index within this string of the first occurrence 'i'
          // starting the search at index 4.  
          idx = str.IndexOf('i', 4); // ==> 5
          Console.WriteLine("- indexOf('i',4) = " + idx);

          // index within this string of the first occurrence of "te".
          idx = str.IndexOf("te"); // ==> 8
          Console.WriteLine("- IndexOf('te') = " + idx);

          Console.Read();
      }
  }

}
Results of running the example:

4.1.4- Substring(..)

SubstringDemo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StringTutorial
{
  class SubstringDemo
  {
      public static void Main(string[] args)
      {
          string str = "This is text";

          // Returns the substring from index 3 to the end of string.
          string substr = str.Substring(3);

          Console.WriteLine("- Substring(3)=" + substr);

          // Returns the substring from index 2, and length 7.
          substr = str.Substring(2, 7);

          Console.WriteLine("- Substring(2, 7) =" + substr);


          Console.Read();
      }
  }

}
Results of running the example:

4.1.5- Replace(...)

Replace(..): replaces all substring with another substring in the current String instance. Return new string.
ReplaceDemo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StringTutorial
{
   class ReplaceDemo
   {
       public static void Main(string[] args)
       {
           String str = "This is text";
   
           // Replace the all character 'i' by 'x'.
           String s2 = str.Replace('i', 'x');

           Console.WriteLine("- s2=" + s2);// ==> "Thxs xs text".

           // Replace all the substrings match "is" by "abc".
   
           String s3 = str.Replace("is", "abc");

           Console.WriteLine("- s3=" + s3);// ==> "Thabc abc text".

           // Replaces the first substring of this string that matches "is" by "abc".
     
           String s4 = ReplaceFirst(str, "is", "abc");

           Console.WriteLine("- s4=" + s4);// ==> "Thabc is text".

           Console.Read();
       }


       // Replace first substring by new substring.
       static string ReplaceFirst(string text, string search, string replace)
       {
           int pos = text.IndexOf(search);
           if (pos < 0)
           {
               return text;
           }
           return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
       }
   }

}
Results of running the example:

4.1.6- Other examples

StringOtherDemo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StringTutorial
{
   class StringOtherDemo
   {
       public static void Main(string[] args)
       {
           String str = "This is text";

           Console.WriteLine("- str=" + str);

           // Return lower case string.
           String s2 = str.ToLower();

           Console.WriteLine("- s2=" + s2);

           // Return upper case string.
           String s3 = str.ToUpper();

           Console.WriteLine("- s3=" + s3);

           // Check string started by "This" or not.
           bool swith = str.StartsWith("This");

           Console.WriteLine("- 'str' startsWith This ? " + swith);

           // A string with whitespace in beginning and end.
           // Note: \t is tab character
           // \n is new line character

           str = " \t CSharp is hot!  \t \n ";

           Console.WriteLine("- str=" + str);

           // Returns a copy of the string, with leading and trailing whitespace omitted.
           String s4 = str.Trim();

           Console.WriteLine("- s4=" + s4);

           Console.Read();
       }
   }

}
Results of running the example:

5- StringBuilder

In C# and every modification in String result creates a new String object. Meanwhile StringBuilder contains in it an array of characters, this array will automatically replaced by a larger array if necessary, and copy the characters from old array. If you must concatenate multiple strings, you should use the StringBuilder, it helps to  increase the efficiency of the program. But it is not necessary if you only concatenate a few strings, you should not abuse StringBuilder in that case.
StringBuilderDemo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StringTutorial
{
  class StringBuilderDemo
  {
      public static void Main(string[] args)
      {

          // Create StringBuilder object
          // with no characters in it and
          // an initial capacity specified by the capacity argument

          StringBuilder sb = new StringBuilder(10);

          // Append the substring Hello ...
          sb.Append("Hello...");
          Console.WriteLine("- sb after appends a string: " + sb);

          // Append a character
          char c = '!';
          sb.Append(c);
          Console.WriteLine("- sb after appending a char: " + sb);

          // Insert a string at index 5
          sb.Insert(8, " CSharp");
          Console.WriteLine("- sb after insert string: " + sb);


          // Remove substring at index 5 and 3 characters
          sb.Remove(5, 3);

          Console.WriteLine("- sb after remove: " + sb);

           // Get string in StringBuilder.
           String s = sb.ToString();

           Console.WriteLine("- String of sb: " + s);

          Console.Read();
      }
  }

}
Results of running the example: