C# String Substring()

Substring in C# string Class returns a new string that is a substring of this string. This method is overloaded by passing the different number of arguments.

C# Substring() Method

  1. String.Substring(Int32) method starts at a specified character position and continues to the end of the string.

  2. String.Substring(Int32, Int32) method starts at a specified character position and has a specified length.

Substring(Int32) example

The following substring example shows how to extract a string using given only one parameter.

string str = null; string retString = null; str = "C# string substring examples"; retString = str.Substring(10); MessageBox.Show(retString);
output
substring examples

String.Substring(Int32, Int32) example

The following C# substring example shows how to extract a string by giving start index and specified length.

string str = null; string retString = null; str = "C# string substring examples"; retString = str.Substring(10,9); MessageBox.Show(retString);
output
substring

C# Substring left

The following example shows how to extract first n number of character from a specified String.

string str = null; string retString = null; str = "C# string substring left examples"; retString = str.Substring(0,10); MessageBox.Show(retString);
output
C# string

Substring Right

The following example shows how to extract last n number of character from a specified String.

string str = null; string retString = null; str = "C# string substring right examples"; retString = str.Substring((str.Length-15), 15); MessageBox.Show(retString);
output
right examples

Retrieve string before or after specified character in C#

The following example shows how to retrieve a string before a specified character or string.

string str = "Retrieve string, before a specified character"; string retString = str.Substring(0, str.IndexOf(",")); MessageBox.Show(retString);
output
Retrieve string

The following example shows how to retrieve a string after a specified character or string.

string str = "Retrieve string, after a specified character"; string retString = str.Substring(str.IndexOf(",") + 2); MessageBox.Show(retString);
output
after a specified character

Retrieve a substring between two strings

The following c# string example shows how to find the string between "substring" and "two".

string str = "How to get a substring between two strings"; int start = str.IndexOf("substring") + "substring".Length; int end = str.IndexOf("two") - start; string retString = str.Substring(start, end); MessageBox.Show(retString);
output
between

ArgumentOutOfRangeException

Typically, an ArgumentOutOfRangeException results from programming error. It is thrown when a function is invoked and at least one of the arguments passed to the method is not null and contains an invalid value that is not a member of the set of values expected for the argument.

In C# String substring() method , this exception raised when Substring() is called with incorrect arguments. When you try to go beyond the string length, or use an argument less than 0 , you will get this exception.

C# substring Performance

C# Strings are immutable , so you can't modify an existing string. If you try to change the string with the substring method then you end up with an entirely new string. You can't ever change an existing string. The operations you perform on a String frequently cause a new allocation of memory. As with all string methods, avoiding them when possible is often the best optimization .

Conclusion

The Substring method is used to extract a portion of a string based on its starting index and optionally a specified length. It returns a new string containing the characters from the original string, starting at the specified index and extending for the specified length, or until the end of the string if the length is not specified.