Computing - C# String Methods
Treating a String as an Array of Characters
We can treat any string or string variable as an array of characters, accessing individual elements by index and retrieving the length.
string word = "Hello";
Console.WriteLine("Length of {0} is {1} characters.", word, word.Length);
Console.WriteLine("The third character of {0} is {1}.", word, word[2]);
foreach (char character in word)
{
Console.WriteLine(character);
}
Length of Hello is 5 characters.
The third character of Hello is l."
H
e
l
l
o
Notice that to retrieve the third character of the word, we used the index 2. That's because C# arrays are zero-based, so the first element has index 0, the second element index 1, and so on.
Changing Case
We can change strings to be all upper case or all lower case. This is particularly useful when comparing strings entered by users with values we may be expecting.
string word = "Hello";
Console.WriteLine("Upper case: {0}", word.ToUpper());
Console.WriteLine("Lower case: {0}", word.ToLower());
// Get text from the user, but convert it to all lower case.
string userChoice = Console.ReadLine().ToLower();
// Now check against our own lower case values.
if (userChoice == "q")
{
Console.WriteLine("You quit.");
}
Upper case: HELLO
Lower case: hello
Q
You quit.
Even though the user enters an upper case 'Q' the ToLower method changes it to 'q' and the if condition is true.
Removing Whitespace
Sometimes, when users enter strings at a prompt, they inadvertently add extra spaces at the start or end of their text. It is such a common problem that there are methods for removing those extra spaces.
string word = " Hello ";
Console.WriteLine("Original: **{0}**", word);
Console.WriteLine("Start: **{0}**", word.TrimStart());
Console.WriteLine("End: **{0}**", word.TrimEnd());
Console.WriteLine("Both: **{0}**", word.Trim());
Original: ** Hello **
Start: **Hello **
End: ** Hello**
Both: **Hello**
Searching for Strings Within Strings
When manipulating strings, it is common to search for particular characters or strings within a larger piece of text. The following methods all help with these tasks.
string phrase = "Hello there. I wonder what strings can do.";
// Find the index of the first occurrence of the character 't'
Console.WriteLine(phrase.IndexOf('t'));
// Find the index of the first occurrence of 't',
// starting at position 7
Console.WriteLine(phrase.IndexOf('t', 7));
// Find the index of the last occurrence of 't'
Console.WriteLine(phrase.LastIndexOf('t'));
// What happens if the string is not there?
Console.WriteLine(phrase.IndexOf("Wow"));
// Does our phrase include the string "wonder"?
Console.WriteLine(phrase.Contains("wonder"));
Adding, Removing and Retrieving Parts of Strings
string phrase = "Hello there. I wonder what strings can do.";
// Show just the string starting at index 6 and of length 5
Console.WriteLine(phrase.Substring(6, 5));
// Remove the string starting at index 5 and of length 6
Console.WriteLine(phrase.Remove(5, 6));
// Replace all occurrences of "there." with "World!"
Console.WriteLine(phrase.Replace("there.", "World!"));
// Insert the string " everyone" at index 11
Console.WriteLine(phrase.Insert(11, " everyone"));
there
Hello. I wonder what strings can do.
Hello World! I wonder what strings can do.
Hello there everyone. I wonder what strings can do.