C# String Methods

This page introduces you to the most commonly used C# String methods, which allow you to manipulate strings effectively, including operations such as searching, replacing, formatting, and extracting substrings, among others.

Section 1. Searching #

In this section, you will learn various string methods that search for a substring in a string.

  • Contains() – Returns true if the string contains the specified string, false otherwise.
  • IndexOf() – Returns the index of the first occurrence of the specified string in the string, or -1 if the string does not occur in the string.
  • LastIndexOf() – Returns the index of the specified string’s last occurrence, or -1 if the string does not occur in the string.
  • StartsWith() – Returns true if the string starts with the specified value, false otherwise.
  • EndsWith() – Returns true if the string ends with the specified value, false otherwise.

Section 2. Trimming #

In this section, you’ll learn how to use various Trim* methods to remove the whitespace or a set of specified characters from a string:

  • Trim() – Returns a new string that is the same as the original string, but with any leading and trailing whitespace characters removed.
  • TrimStart() – Removes all whitespace characters at the beginning of the current string.
  • TrimEnd() – Removes all whitespace characters at the end of the current string.

Section 3. Padding #

In this section, you will learn how to pad a string with specified characters to achieve a desired length.

  • PadLeft() – Returns a new string that is the specified length, with the specified character padded to the left.
  • PadRight() – Returns a new string that is the specified length, with the specified character padded to the right.

Section 4. Extracting #

In this section, you will learn how to extract a portion of a string and split the string into an array of substrings:

  • Substring() – Returns a new string that is a substring of the original string, starting at the specified index and with the specified length.
  • Split() – Returns an array of strings that are the result of splitting the string at each occurrence of the specified separator string.

Section 5. Replacing & Inserting #

In this section, you’ll learn how to replace a substring in a string with a new one:

  • Replace – Returns a new string with all occurrences of the specified old value replaced with the specified new value.
  • Insert() – Returns a new string with a string inserted at a specified index of the current string.

Section 6. Changing Cases #

In this section, you’ll learn how to convert a string to lowercase or uppercase:

  • ToLower() – Returns a lowercase version of the string as a new copy.
  • ToUpper() – Returns an uppercase version of the string as a new copy.

Section 7. Concatenating #

In this section, you’ll learn how to use the Join() and Concat() methods to concatenate two or more strings into a single string:

  • Join() – concatenate two or more strings in a collection into a string with a specified separator.
  • Concat() – concatenate two more strings into a string.

Was this helpful?