C# Strings
C# Strings
✓Strings are used for storing text. Example
✓A string variable contains a collection of string greeting = "Hello" ;
characters surrounded by double quotes:
String Methods
String Methods
A string in C# is actually an object, which contain properties and methods that can perform
certain operations on strings.
String Class (System) | Microsoft Learn
String Length
✓The length of a string can be found with the Example:
Length property: string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
[Link] ( "The length of the txt: " +
[Link] );
ToUpper() and ToLower()
✓which returns a copy of the string converted Example:
to uppercase or lowercase:
string txt = "Hello World" ;
[Link] ( [Link] ());
// Outputs "HELLO WORLD"
[Link] ( [Link] ());
// Outputs "hello world"
String Concatenation
✓The + operator can be used between strings ✓You can also use the [Link]() method to concatenate
to combine them. This is called concatenation: two strings:
Example: Example:
string firstName = "John " ; string firstName = "John " ;
string lastName = "Doe" ; string lastName = "Doe" ;
string name = firstName + lastName ; string name = string .Concat ( firstName , lastName );
[Link] (name); [Link] (name);
String Interpolation
✓Another option of string concatenation, is string interpolation, which substitutes values of
variables into placeholders in a string. Note that you do not have to worry about spaces, like
with concatenation:
Example:
string firstName = "John" ;
string lastName = "Doe" ;
string name = $"My full name is: { firstName } { lastName } " ;
[Link] (name);
C# compare two strings - Equals()
✓In C#, we can make comparisons between two strings using the Equals() method.
✓The Equals() method checks if two strings are equal or not.
Example:
// create string
string str1 = "C# Programming" ;
string str2 = "C# Programming" ;
string str3 = "C++ Programming" ;
// compare str1 and str2
Boolean result1 = [Link](str2);
[Link] ( "string str1 and str2 are equal: " + result1); //true
//compare str1 and str3
Boolean result2 = [Link](str3);
[Link] ( "string str1 and str3 are equal: " + result2); // false
Trim()
✓The trim() method removes whitespace or specified character from both sides of a string.
✓The trim() method does not change the original string.
Access Strings
✓You can access the characters in a string by Example: prints the first character in myString:
referring to its index number inside square
string myString = "Hello" ;
brackets [].
[Link] ( myString [0]); // Outputs "H"
✓Note: String indexes start with 0: [0] is the
first character. [1] is the second character, etc.
Access Strings - IndexOf()
✓You can also find the index position of a Example:
specific character in a string, by using the string myString = "Hello" ;
IndexOf() method:
[Link] ( [Link] ( "e" )); // "1"
Access Strings - Substring()
✓Another useful method is Substring(), which Example:
extracts the characters from a string, starting // Full name
from the specified character position/index,
and returns a new string. string name = "John Doe" ;
// Location of the letter D
✓This method is often used together with
IndexOf() to get the specific character int charPos = [Link] ( "D" );
position: // Get last name
✓Substring(start index, how many character) string lastName = [Link] ( charPos );
// Print the result
[Link] ( lastName );
Immutability of String Objects
✓In C#, strings are immutable. This means, // create string
once we create a string, we cannot change string str = "Hello " ;
that string.
✓Here, we have created a string variable named str.
✓To understand it, consider an example: ==> The variable holds the string "Hello ".
// add another string "World"
// to the previous string example
✓Creates a new string object, gives it a value
"Hello World", and stores it in str. str = string .Concat ( str , "World" );
✓Here, we are using the Concat() method to add the
✓The original string, "Hello ", that was assigned string "World" to the previous string str.
to str is released for garbage collection
because no other variable holds a reference to
it.
String Escape Sequences - Special Characters
✓The escape character is used to escape some ✓To solve this issue, we use the escape
of the characters present inside a string. In character \" in C#.
other words, we use escape sequences to
insert special characters inside the string.
// use the escape character
string str = "This is the \ "String \ " class." ;
// include double quote
string str = "This is the " String " class" ;
✓Since strings are represented by double
quotes, the compiler will treat "This is the " as
the string. And the above code will cause an
error.
String Escape Sequences - Special Characters
Escape Sequence Character Name
\' single quote
\" double quote
\\ backslash
\0 null
\n new line
\t horizontal tab
Methods of C# string
Methods Description
checks if the string ends with the
EndsWith()
Format() returns a formatted string given string
Split() splits the string into substring
returns the position of the
IndexOf()
specified character in the string
Substring() returns substring of a string
Compare() compares string objects Remove() returns characters from a string
replaces the specified old ToUpper() converts the string to uppercase
Replace() character with the specified new
character ToLower() converts the string to lowercase
checks whether the string checks if the string begins with
Contains() StartsWith()
contains a substring the given string
joins the given strings using the
Join() ToCharArray() converts the string to a char array
specified separator
removes any leading and trailing LastIndexOf() returns index of the last
Trim() occurrence of a specified string
whitespaces
Resources
[Link]
C# String (With Examples)