7.5. String Methods

C# provides many useful methods for string objects. Recall that a method is a function that “belongs to” a specific object. Methods will typically result in some operation being carried out on the data within an object. For strings, this means that our methods will typically transform the characters of the given string in some way. This transformation will either be a modified copy of the original string, or a new instance of the string.

As we have learned, strings are immutable. Therefore, string methods will not change the value of a string itself, but instead will return a new string that is the result of the given operation.

We saw this behavior in the ToLower example.

Example

1
2
3
4
string nonprofit = "LaunchCode";

Console.WriteLine(nonprofit.ToLower());
Console.WriteLine(nonprofit);

Console Output

launchcode
LaunchCode

While nonprofit.ToLower() evaluated to "launchcode", the value of nonprofit was left unchanged. This will be case for each of the string methods.

7.5.1. Common String Methods

Here we present the most commonly-used string methods. You can find documentation for other string methods at:

Common String Methods

Method

Syntax

Description

IndexOf

stringName.IndexOf(substr)

Returns the index of the first occurrence of the substring in the string, and returns -1 if the substring is not found.

Contains

stringName.Contains(substr)

Returns a boolean value if a specified string value is present.

ToLower

stringName.ToLower()

Returns a copy of the given string, with all uppercase letters converted to lowercase.

ToUpper

stringName.ToUpper()

Returns a copy of the given string, with all lowercase letters converted to uppercase.

Trim

stringName.Trim()

Returns a copy of the given string with the leading and trailing whitespace removed.

Replace

stringName.Replace(searchChar, replacementChar)

Returns a copy of stringName with the all occurrence of searchChar replaced by replacementChar.

Substring

stringName.Substring(i, j)

Returns the substring consisting of characters from index i for a length of j.

Split

stringName.Split(delimiter)

Splits the string into sections at each delimiter and stores the sections as elements in an array.

Insert

stringName.Insert(i, substring)

Returns new stringName in which a specific substring has been inserted at index i.

Remove

stringName.Remove(i, j)

Returns new string after certain number of characters have been deleted from current string.

ToCharArray

stringName.ToCharArray()

Copies characters of string into character array.

ToString

stringName.ToString()

Returns a string that represents the current object.

Tip

String methods can be combined in a process called method chaining. Given word = "C Sharp";, word.ToUpper() returns C SHARP. What would word.Substring(1,4).ToUpper() return? Try it at repl.it.

7.5.2. Check Your Understanding

Follow the links in the table above for the Replace, Substring, and Trim methods. Review the content and then answer the following questions.

Question

What is printed by the following code?

1
2
3
4
string language = "C Sharp";
language.Replace('C', 'Q');
language.Substring(0,5);
Console.WriteLine(language);
  1. "C Sharp"

  2. "Q Sharp"

  3. "Q Sha"

  4. "C Shar"

Question

Given string language = 'C Sharp';, what does language.Substring(1,4) return?

  1. " Sha"

  2. "h"

  3. "Shar"

  4. "C Sh"

Question

What is the value of the string printed by the following program?

1
2
3
4
string org = "  The LaunchCode Foundation ";
string trimmed = org.Trim();

Console.WriteLine(trimmed);
  1. "  The LaunchCode Foundation "

  2. "The LaunchCode Foundation"

  3. "TheLaunchCodeFoundation"

  4. " The LaunchCode Foundation"

..ans: “The LaunchCode Foundation”