Tutorialsteacher

Follow Us

C# Strings

In C#, a string is a series of characters that is used to represent text. It can be a character, a word or a long passage surrounded with the double quotes ". The following are string literals.

Example: String Literals
"S"
"String"
"This is a string."

C# provides the String data type to store string literals. A variable of the string type can be declared and assign string literal, as shown below.

Example: String Type Variables
string ch = "S";
string word = "String";
string text = "This is a string.";

The maximum size of a String object in memory is 2GB or about 1 billion characters. However, practically it will be less depending upon CPU and memory of the computer.

There two ways to declare a string variable in C#. Using System.String class and using string keyword. Both are the same and make no difference. Learn string vs String for more info.

Example: String and string
string str1 = "Hello"; // uses string keyword
 
String str2 = "Hello"; // uses System.String class

In C#, a string is a collection or an array of characters. So, string can be created using a char array or accessed like a char array.

Example: String as char Array
char[] chars = {'H','e','l','l','o'};

string str1 = new string(chars);  
String str2 = new String(chars); 

foreach (char c in str1)
{
    Console.WriteLine(c);
}

Special Characters

A text in the real world can include any character. In C#, because a string is surrounded with double quotes, it cannot include " in a string. The following will give a compile-time error.

Example: Invalid String
string text = "This is a "string" in C#.";

C# includes escaping character \ (backslash) before these special characters to include in a string.

Use backslash \ before double quotes and some special characters such as \,\n,\r,\t, etc. to include it in a string.

Example: Escape Char \
string text = "This is a "string" in C#.";
string str = "xyzdef\rabc";
string path = "\\mypc\ shared\project";

Verbatim Strings

It is tedious to prefix \ to include every special characters. Verbatim string in C# allows a special characters and line brakes. Verbatim string can be created by prefixing @ symbol before double quotes.

Example: Escape Sequence
string str = @"xyzdef
abc";
string path = @"\mypcsharedproject";
string email = @"[email protected]";

The @ symbol can also be used to declare a multi-line string.

Example: Multi-line String
string str1 = "this is a 
" + 
        "multi line 
" + 
        "string";
		
// Verbatim string
string str2 = @"this is a 
        multi line 
        string";

Please note that you cannot use a backslash to allow " in a varbatim string. If you wish to include @, then use double double-quotes "" to include " in a verbatim string.

string text = "This is a "string" in C#."; // valid
string text = @"This is a "string." in C#."; // error
string text = @"This is a "string" in C#."; // error
string text = @"This is a ""string"" in C#."; // valid

String Concatenation

Multiple strings can be concatenated with + operator.

Example: String Concatenation
string name = "Mr." + "James " + "Bond" + ", Code: 007";
 
string firstName = "James";
string lastName = "Bond";
string code = "007";
 
string agent = "Mr." + firstName + " " + lastName + ", Code: " + code;

A String is immutable in C#. It means it is read-only and cannot be changed once created in the memory. Each time you concatenate strings, .NET CLR will create a new memory location for the concatenated string. So, it is recommended to use StringBuilder instead of string if you concatenate more than five strings.

String Interpolation

String interpolation is a better way of concatenating strings. We use + sign to concatenate string variables with static strings.

C# 6 includes a special character $ to identify an interpolated string. An interpolated string is a mixture of static string and string variable where string variables should be in brackets.

Example: String Interpolation
string firstName = "James";
string lastName = "Bond";
string code = "007";
 
string fullName = $"Mr. {firstName} {lastName}, Code: {code}";

In the above example of interpolation, $ indicates the interpolated string, and includes string variable to be incorporated with a string.

Use two braces, {{ or }} to include { or } in a string.