C# Strings
C# Strings
Strings are used for storing text.
A string variable contains a collection of characters surrounded by double quotes.
Example:-
Create a variable of type string and assign it a value:
string greeting = "Hello";
String Length:-
A string in C# is actually an object, which contain properties and methods that can perform certain operations on strings.
Example:-
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Console.WriteLine("The length of the txt string is: " + txt.Length);
Other Methods
There are many string methods available, for example ToUpper() and ToLower(), which returns a copy of the string converted to uppercase or lowercase:
Example:-
string txt = "Hello World";
Console.WriteLine(txt.ToUpper()); // Outputs "HELLO WORLD"
Console.WriteLine(txt.ToLower()); // Outputs "hello world"
String Concatenation:-
The + operator can be used between strings to combine them. This is called concatenation.
Example:-
string firstName = "John ";
string lastName = "Doe";
string name = firstName + lastName;
Console.WriteLine(name);
Note that we have added a space after "John" to create a space between firstName and lastName on print.
You can also use the string.Concat() method to concatenate two strings.
Example:-
string firstName = "John ";
string lastName = "Doe";
string name = string.Concat(firstName, lastName);
Console.WriteLine(name);
String Interpolation:-
Another option of string concatenation, is string interpolation, which substitutes values of variables into placeholders in a string.
Example:-
string firstName = "John";
string lastName = "Doe";
string name = $"My full name is: {firstName} {lastName}";
Console.WriteLine(name);
Note: you have to use the dollar sign ($) when using the string interpolation method.
String interpolation was introduced in C# version 6.
Access Strings:- You can access the characters in a string by referring to its index number inside square brackets [].
Example:-
string myString = "Hello";
Console.WriteLine(myString[0]); // Outputs "H"
Note: String indexes start with 0: [0] is the first character. [1] is the second character, etc.
Example:-
string myString = "Hello";
Console.WriteLine(myString[1]); // Outputs "e"
You can also find the index position of a specific character in a string, by using the IndexOf() method:
Example:-
string myString = "Hello";
Console.WriteLine(myString.IndexOf("e")); // Outputs "1"
Another useful method is Substring(), which extracts the characters from a string, starting from the specified character position/index, and returns a new string. This method is often used together with IndexOf() to get the specific character position:
Example:-
// Full name
string name = "John Doe";
// Location of the letter D
int charPos = name.IndexOf("D");
// Get last name
string lastName = name.Substring(charPos);
// Print the result
Console.WriteLine(lastName);
Special Characters
Because strings must be written within quotes, C# will misunderstand this string, and generate an error:
string txt = "We are the so-called "Vikings" from the north.";
The solution to avoid this problem, is to use the backslash escape character.
The backslash (\) escape character turns special characters into string characters:
Escape character Result Description
\' ' Single quote
\" " Double quote
\\ \ Backslash
The sequence \" inserts a double quote in a string:
Example:-
string txt = "We are the so-called \"Vikings\" from the north.";
The sequence \' inserts a single quote in a string:
Example:-
string txt = "It\'s alright.";
The sequence \\ inserts a single backslash in a string:
Example:-
string txt = "The character \\ is called backslash.";
Other useful escape characters in C# are:
Code Result
\n New Line
\t Tab
\b Backspace
C# uses the + operator for both addition and concatenation. If you add two strings, the result will be a string concatenation:
Example:-
string x = "10";
string y = "20";
string z = x + y; // z will be 1020 (a string)