The C# language provides two kinds of strings, called "String" (or "string", an alias) and "StringBuilder".
Each of these stores characters in Unicode, 2 bytes per character, but they do so differently. It is not easy to find details of the internal implementation, but apparently:
Unicode values can be specifed as \udddd or \xdddd, where dddd is a 4-digit hexadecimal number. For instance, the character 'X' is \u0058, or we could convert it from an integer value as a cast: (char)88 where 88 (decimal) is 58 (hexadecimal). (The ASCII set is included in Unicode at its beginning as values starting with two zeros.)
String type
String type is a reference type. (The string itself may be huge and therefore must exist on the heap.) It is in the System namespace.
The equality and inequality operators for String will, however, compare the values of the String objects.
A String object is immutable; once created, it cannot be changed. Although we can write code that appears to change the value of a string, as in
string S = "Hello"; S = S + " there"; // S is now "Hello there"
what happens is that a new String is created to hold the new value, the new String is assigned to S, and the original String "Hello" is marked (in the heap) as garbage to be collected.
There are various ways to declare a string (different constructors):
// Declare without initializing. string message1; // Initialize to null. string message2 = null; // Initialize as an empty string. // Use the Empty constant instead of the literal "". string message3 = System.String.Empty; // Initialize with a regular string literal. // Notice the escape sequence, "\\" to represent '\'. string oldPath = "c:\\Program Files\\Microsoft Visual Studio 8.0"; // Initialize with a verbatim string literal. // Here the '@' at the beginning allows us to avoid using // escape sequences. string newPath = @"c:\Program Files\Microsoft Visual Studio 9.0"; // Use System.String if you prefer. System.String greeting = "Hello World!"; // In local variables (i.e. within a method body) // you can use implicit typing: var will be a String. var temp = "I'm still a strongly-typed System.String!"; // Use a const string to prevent 'message4' from // being used to store another string value. const string message4 = "You can't get rid of me!"; // Initialize from an array of characters. char[] letters = { 'A', 'B', 'C' }; string alphabet = new string(letters);
The String class has two properties. Suppose S is a String.
The class also has a vast number of methods, including:
This will make a copy of a string.
This will create a string = S1 + S2; this can also be done with +.
This asks if this string is equal to S.
This creates a string = part of this string, starting at position M, N characters long
This splits a string into a number of substrings separated by characters in separator. Delimiter characters are not included in the substrings. If separator is null, white-space characters are counted as delimiters.
This replaces a format item (such as {0}) in the string F by the string representation of the object F. For instance:
String s = String.Format("The current temperature is {0} degrees.", CurrentTemp);
There are various options for the format item. This is the same mechanism used by methods such as WriteLine.
and many more.
The type of a string literal is String. If we pass a literal, "hello", to a method such as Console.WriteLine(), a String is created to contain the value "hello".
StringBuilder class
StringBuilder is a reference type and is in the System.Text namespace.
A StringBuilder instance is mutable. We can make changes to it, making it longer or shorter, changing a character in the middle, etc. Unless the string reaches its present capacity and must be enlarged, there will not be new StringBuilder instances created and old ones destroyed.
There are various ways to declare a StringBuilder instance (different constructors). Here are some:
string value = "An ordinary string"; int capacity = 0xFFFF; // Instantiate a StringBuilder but not initialize it. StringBuilder sb0; // Instantiate a StringBuilder from a string. StringBuilder sb1 = new StringBuilder(value); // Instantiate a StringBuilder from string and define a capacity. StringBuilder sb2 = new StringBuilder(value, capacity);
The StringBuilder class has various properties. Suppose S is a StringBuilder.
The class has a vast number of methods, such as:
and many more.
When to use String or StringBuilder
If you think you are going to make a lot of modifications to a string, use the StringBuilder type.
There can be wasted space with a StringBuilder, as Capacity >= Length.