News & UpdatesProgrammingWeb programming StoreMy Projects

C# Tutorial – 06 – String

The string data type is used to store string constants, which are delimited by double quotes.

string a = "Hello";

String concatenation

The plus sign is used to combine two strings. It is known as the concatenation operator (+) in this context. It also has an accompanying assignment operator (+=), which appends a string to another and creates a new string.

string b = a + " World"; // Hello World
a += " World";           // Hello World

Escape characters

A statement can be broken up across multiple lines, but a string constant must be on a single line. In order to divide it, the string constant has to first be split up using the concatenation operator.

string c
         = "Hello " +

To add new lines into the string itself, the escape character “\n” is used.

string c = "Hello\nWorld";

This backslash notation is used to write special characters, such as the backslash itself or a double-quote. Among the special characters is also a Unicode character notation for writing any character.

\nnewline\fform feed
\thorizontal tab\aalert sound
\vvertical tab\’single quote
\bbackspace\”double quote
\rcarriage return\\backslash
\0null character\uFFFFUnicode character (4-digit hex number)

Escape characters can be ignored by adding an “@” symbol before the string. This is called a verbatim string and can for example be used to make file paths more readable.

string e = "c:\\Windows\\System32\\cmd.exe";
string f = @"c:\Windows\System32\cmd.exe";

String compare

The way to compare two strings is simply by using the equal to operator. This will not compare the memory addresses, as in some other languages such as Java.

bool c = (a == b); // true

String members

The string class has a lot of useful members. For example, methods like Replace, Insert and Remove. An important thing to note is that there are no methods for changing a string. Methods that appear to modify a string actually always return a completely new string. This is because the string class is immutable. The content of a string variable cannot be changed, unless the whole string is replaced.

string a = "String";
string b = a.Replace("i", "o"); // Strong
       b = a.Insert(0, "My ");  // My String
       b = a.Remove(0, 3);      // ing
       b = a.Substring(0, 3);   // Str
       b = a.ToUpper();         // STRING
int    i = a.Length;            // 6

StringBuilder class

StringBuilder is a mutable string class. Because of the performance cost associated with replacing a string, the StringBuilder class is a better alternative when a string needs to be modified many times.

System.Text.StringBuilder sb = new System.Text.StringBuilder("Hello");

The class has several methods that can be used to manipulate the actual content of a string, such as: Append, Remove and Insert.

sb.Append(" World");   // Hello World
sb.Remove(0, 5);       // World
sb.Insert(0, "Bye");   // Bye World

To convert a StringBuilder object back into a regular string, the ToString method is used.

string s = sb.ToString(); // Bye World