C# String Methods
String Methods
You have already used some string methods, like ToUpper(), IndexOf() and Substring().
C# has many more. In this chapter, you will learn a few that you will use often: check if a string contains some text, check how it starts or ends, replace text, and remove extra spaces.
The Contains() method checks if a string contains some text. It returns True or False:
Example
string txt = "I love apples";
Console.WriteLine(txt.Contains("apples")); // True
Console.WriteLine(txt.Contains("pears")); // False
Console.WriteLine(txt.Contains("Apples")); // False
Note that Contains() is case-sensitive: "Apples" with an uppercase A is not the same as "apples".
StartsWith() and EndsWith()
The StartsWith() method checks if a string starts with some text, and EndsWith() checks if it ends with some text.
For example, a file name that ends with .jpg is an image:
Example
string fileName = "photo.jpg";
Console.WriteLine(fileName.StartsWith("photo")); // True
Console.WriteLine(fileName.EndsWith(".jpg")); // True
Console.WriteLine(fileName.EndsWith(".png")); // False
Replace()
The Replace() method replaces some text with other text. It replaces every match in the string:
Example
string colors = "red, blue, red";
Console.WriteLine(colors.Replace("red", "green")); // green, blue, green
Trim()
The Trim() method removes spaces from the start and the end of a string. This is useful when someone types a name with extra spaces.
In the example below, we print brackets around the text, so you can see where the spaces are:
Example
string name = " Liam ";
Console.WriteLine("[" + name + "]"); // [ Liam ]
Console.WriteLine("[" + name.Trim() + "]"); // [Liam]
The Original String Does Not Change
String methods do not change the original string. They return a new string with the result:
Example
string txt = "I like cats";
string newTxt = txt.Replace("cats", "dogs");
Console.WriteLine(txt); // I like cats
Console.WriteLine(newTxt); // I like dogs
To keep the result, store it in a variable. It can also be the same variable: txt = txt.Replace("cats", "dogs");
Common String Methods
Here are some common string methods:
| Method | Description |
|---|---|
Contains(text) |
Returns True if the string contains the text |
StartsWith(text) |
Returns True if the string starts with the text |
EndsWith(text) |
Returns True if the string ends with the text |
IndexOf(text) |
Returns the position of the first match, or -1 if the text is not found |
Replace(oldText, newText) |
Returns a new string where every oldText is replaced with newText |
Substring(start) |
Returns the characters from position start to the end |
Substring(start, length) |
Returns length characters, starting at position start |
ToUpper() |
Returns the string in uppercase letters |
ToLower() |
Returns the string in lowercase letters |
Trim() |
Returns the string without spaces at the start and the end |
TrimStart() |
Returns the string without spaces at the start |
TrimEnd() |
Returns the string without spaces at the end |