Substring in C#

Learn via video courses
Topics Covered

Overview

Substrings are, as their name implies, parts of a string. They provide a means to extract particular text chunks from a larger string in C#. When you need to extract important information from a lengthy text or alter strings for different purposes, this functionality comes in quite handy. The Substring() method in C# is used to extract a portion of a given string. It allows you to choose the starting index where the extraction will start and, optionally, the length of the substring to be extracted. If no length is specified, the method will extract the remaining characters from the beginning of the string to the end.

What is Substring in C#?

A C# substring is a segment of a string that is taken from a bigger string. The substring normally begins at a particular index and can continue for a predetermined amount of time or all the way to the end of the original text. These actions are possible in C# thanks to the built-in Substring() method.

You may alter strings by removing certain text fragments using the Substring() method. This is very helpful when you just need to interact with a portion of the content of a string. C# Substring is useful for a variety of purposes, including removing URLs from text and analyzing user input.

An overview of the Substring() method is provided below:

Syntax:

Here in the above syntax:

  • original_String:
    The string from which a C# substring should be derived.
  • start_Index:
    The index at which to start extracting the C# substring. The index is zero-based, hence the index for the first character is 0.
  • length:
    The length of the retrieved substring, if provided. The substring will run from the start_Index to the end of the original string if it is not specified.

Example:

Output

In this case, the C# substring "World!" is produced by the Substring() method's extraction of the piece of the original string beginning at index 7, which corresponds to the character "W", and continuing to the end of the string.

String.Substring Method (startIndex)

You can extract a substring from a given text in C# by using the Substring() method, which allows you to start at a certain index and go all the way to the end of the string. Here is the syntax, along with instructions for using it:

Syntax

In the above syntax, start_Index is the index at which the extraction of the substrings should start. Since the index is zero-based, the string's initial character has an index of 0. The characters from the specified start_Index to the end of the old string are included in the new string that this function returns.

Example

Output

In this example, the Substring(7) call extracts the substring starting from index 7 in the original string, which corresponds to the character 'W'. The extracted C# substring includes all characters from index 7 to the end of the string, resulting in the output "World!".

Remember that the Substring() method doesn't modify the original string; it returns a new string containing the extracted substring. If you want to extract a specific range of characters, you can use the overload of the Substring() method, which takes both the start_Index and the length as parameters. This variant allows you to extract a C# substring of a specified length starting from the given index.

String.Substring Method (int startIndex, int length)

You can also extract a substring from a given string, starting at a specific index and extending for a defined length, using the overload of the C# String.Substring() method. The syntax and instructions for using this overload are provided below:

Syntax

In the above syntax, start_Index is the index at which the extraction of the substrings should start. Since the index is zero-based, the string's initial character has an index of 0. The length specifies how many characters should be included in the extracted substring. This method returns a new string that begins at the provided start_Index and ends with the specified length in characters.

Example

Output

In this example, the Substring(7, 5) method extracts the substring beginning at the letter 'W' at index 7 in the original string and including the next 5 characters. As a result, the output is "World".

It's vital to remember that the Substring() method provides a new string that contains the extracted substring rather than altering the original string. You can exactly specify the starting index and length of the substring you wish to extract from the source text using this method override.

Examples

Get the First N Characters Substring from A String in C#

You can easily get the first n characters of a string in C# by using the String.Substring() method.

Example

Output

In this example, the function original_String.Substring(0, n) pulls a substring from the original string beginning at index 0 and extending for n characters. Since the first five letters of the original string are "Hello".

The initial character of the string is at index zero since the Substring() method employs zero-based indexing. To prevent an ArgumentOutOfRangeException, make sure that n is inside the bounds of the original string.

Get a Substring After or Before a Character in C#

You can use the String.IndexOf method in C# to find the position of a certain character in a string, and then use the String.Substring() method to extract the substring before or after that character.

Example

Output

In the above example, we create a string, find the index of a delimiter (,), extract substrings before and after it, and output the results. The delimiter's index is found using IndexOf. Substrings are extracted using Substring.

Get a Substring Between Two Strings in C#

In C#, you can use the String.IndexOf() method to determine the locations of the starting and ending strings before using the String to obtain a substring between them. To extract the desired substring between them, use the substring technique.

Example

Output

In the above example, the code finds where the start_String ("quick") begins in the original text. Then, it looks for where the end_String ("jumps") starts, but this search begins right after the start_String start_String. If it finds both the start_String and end_String, the code calculates the length between them. Finally, it takes out and shows the part of the text between these two words.

Please be aware that this method expects the start_String and end_String to be in the original string in that order. The spaces between the start and finish texts are also included in the extracted substring. If you wish to include or remove the spaces, you might need to modify the computation.

Get the Last 4 Characters of A String in C#

In C#, you can use the String.Substring() method and the string's Length property to retrieve the final four characters of a string.

Example

Output

To get the last four characters of the original string, the code calculates where their starting position should be. It uses this formula: starting position = total characters in original string - count of last characters. Then, it takes these last four characters and shows them using the Substring function.

To prevent negative indexes from causing an ArgumentOutOfRangeException, double-check that start_Index is larger than or equal to 00

Conclusion

  • A C# substring is a segment of a string that is taken from a bigger string.
  • You can extract a C# substring from a given text by using the Substring() method, which allows you to start at a certain index and go all the way to the end of the string.
  • You can also extract a substring from a given string, starting at a specific index and extending for a defined length, using the overload of the C# String.Substring() method.
  • If no length is specified, the method will extract the remaining characters from the beginning of the string to the end.
  • You can use the String.IndexOf method in C# to find the position of a certain character in a string.