English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# Strings (String)

In C#, strings are a series of characters used to represent text. It can be a character, a word, or a long paragraph enclosed in double quotes. The following are string literals.

"S"
"String"
"This is a string."

C# provides the String data type to store string literals. You can declare string type variables and assign string literals as shown below.

string ch = "S";
string word = "String";
string text = "This is a string.";

The maximum size of a string object in memory is2GB, approximately10Billion characters. However, in practical applications, it depends less on the computer's CPU and memory.

There are two ways to declare string variables in C#. Using the System.String class and the string keyword. Both are the same, without any difference.

string str1 = "Hello"; // Using string keyword
 
String str2 = "Hello"; // Using System.String class

In C#, strings are a collection or array of characters. Therefore, you can create a string using a char array, and you can also access a string like a 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

Text in the real world can contain any character. In C#, because strings are enclosed in double quotes, they cannot contain (”) within the string. The following content will result in a compile-time error.

string text = "This is a "string" in C#.";

C# includes the escape character \ (backslash) before these special characters in the string.

Use a backslash \ before double quotes and some special characters (such as \, \n, \r, \t, etc.) to include them within the string.

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

However, adding an \ before each special character can be very cumbersome. Strings prefixed with @ are intended to be treated as literals and cannot escape any characters.

string str = @"xyzdef\rabc";
string path = @"\\mypc\shared\project";
string email = @"[email protected]";

Use @ and \ to declare multiline strings.

string str = @"this is a \
multi line \
string";

Please note that backslashes must be used within strings to allow the appearance of double quotes. The @ symbol is only applicable to special characters in C#.

string text = @"This is a \ // 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 + operator concatenation.

string name = "Mr." + "James" + "Bond" + "Code: 00"7";
 
string firstName = "James";
string lastName = "Bond";
string code = "007";
 
string agent = "Mr." + firstName + "" + lastName + "Code: 00" + code;

Strings in C# are immutable. This means they are read-only and cannot be changed once created in memory. Each time strings are concatenated, the .NET CLR creates a new memory location for the concatenated string. Therefore, if five or more strings are concatenated, it is recommended to use StringBuilder instead of strings.

string interpolation

String interpolation is a better method for concatenating strings. We use+The symbol connects string variables with static strings.

C#6A special character $ is used to identify interpolated strings. Interpolated strings are a mix of static strings and string variables, where string variables should be placed within {} brackets.

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

In the above interpolation example, $ represents the interpolated string, and {} includes the string variables to be merged with the string.

Use two curly braces '{{' or '}}' to enclose { or } in strings.