Substring in C#

A Substring in C# is a contiguous sequence of characters within a string. In other words, a substring in C# is a portion of a string. The string class in C# represents a string. Here is a list of commonly asked questions related to C# substring with code examples.
  1. Get a substring from a string 
  2. Get a substring of first n characters from a string 
  3. Get a substring after a character 
  4. Get a substring before a character 
  5. Get a substring with start and end index 
  6. Get a substring from index to end
  7. Get a substring between two strings
To learn more about C# strings, check out String In C# tutorial to learn more about strings and how to work with strings in C#.
Use of CSharp Substring method  

1. C# String.Substring method 

In C# and .NET, a string is represented by the String class. The String. Substring method retrieves a substring from a string instance in C#. The method has the following two overloaded forms. 
  1. Substring(Int32) - Retrieves a substring from the specified position to the end of the string.
  2. Substring(Int32, Int32 - Retrieves a substring from this instance from the specified position for the specified length.

2. Get first n characters substring from a string in C# 

String characters are zero-indexed. Means, the position of the first characters in a string starts at 0th position. 
Let’s say, you want to get a substring of the first 12 characters from a string. We can use the Substring method and pass it starting index 0 and length of the substring 12 to get the first 12 char substring from a string. 
The following code snippet is an example of retrieving a 12 chars substring from a string.
  1. // A long string    
  2. string bio = "Mahesh Chand is a founder of C# Corner. Mahesh is also an author, speaker, and software architect. Mahesh founded C# Corner in 2000.";    
  4. // Get first 12 characters substring from a string    
  5. string authorName = bio.Substring(0, 12);    
  6. Console.WriteLine(authorName);
Now, if you want to retrieve a substring everything after 12th position. You can simply pass the starting position in the string to get the rest of the string as a substring. The following code snippet gets a substring that has all characters after the 12th position in a string.
  1. // Get everything else after 12th position     
  2. string authorBio = bio.Substring(12);    
  3. Console.WriteLine(authorBio);
The complete code example is listed in Listing 1.
  1. using System;    
  2. class Program    
  3. {    
  4.     static void Main(string[] args)    
  5.     {    
  6.         // A long string    
  7.         string bio = "Mahesh Chand is a founder of C# Corner. Mahesh is also an author, " +    
  8.             "speaker, and software architect. Mahesh founded C# Corner in 2000.";    
  10.         // Get first 12 characters substring from a string    
  11.         string authorName = bio.Substring(0, 12);    
  12.         Console.WriteLine(authorName);    
  14.         // Get everything else after 12th position     
  15.         string authorBio = bio.Substring(12);    
  16.         Console.WriteLine(authorBio);    
  18.         Console.ReadKey();    
  19.     }    
  20. }
Listing 1.
The output of Listing 1 looks like Figure 1.
Substring C#
Figure 1.

3. Get a substring with a start and end index

The first parameter of the Substring method is the starting index of the substring. The second parameter is the number of characters including whitespaces. You can use the String. Length to find out the end index of a string.
The following code snippet gets a substring starting at index 8 till the end of the string.
  1. // Get a string between a start index and end index    
  2. string str = "How to find a substring in a string";    
  3. int startIndex = 7;    
  4. int endIndex = str.Length - 7;    
  5. string title = str.Substring(startIndex, endIndex);    
  6. Console.WriteLine(title);
You can also pass the second argument of Substring to get a number of characters. The following code snippet gets a substring of 15 characters starting at index 8.
  1. // Get next 15 characters from starting index    
  2. string title15 = str.Substring(startIndex, 15);
The output looks like Figure 2.
Figure 2.

4. Get a substring after or before a character in C#

You can use the Substring method to find a substring before the first occurrence of a specified character. You can pass the starting index as 0 and length as the position of the specified characters.
The following code snippet retrieves a substring that is before the first occurrence of a comma.
  1. // Get a substring after or before a character    
  2. string authors = "Mahesh Chand, Henry He, Chris Love, Raj Beniwal, Praveen Kumar";            
  3. string stringBeforeChar = authors.Substring(0, authors.IndexOf(","));            
  4. Console.WriteLine(stringBeforeChar);
The following code snippet retrieves a substring that is after the first occurrence of a comma.
  1. // Get a substring after a character     
  2. string stringAfterChar = authors.Substring(authors.IndexOf(",") + 2);    
  3. Console.WriteLine(stringAfterChar);

5. Find occurrence of substrings in a string in C#

You can find the position of a substring in a string by using String.IndexOf method. The following code snippet returns the position of a substring in a string.
  1. int firstStringPosition = authors.IndexOf("Henry");

6. Get a substring between two strings in C#

You can use Substring method to find a substring between two strings. First, you need to find the position of the two strings in the string. Then use first string position as the starting position and find the length of the string by subtracting position of the first string from the position of the second string.
The following code snippet retrieves a substring between ‘Henry’ and ‘Beniwal’.
  1. // Get a substring after or before a character    
  2. string authors = "Mahesh Chand, Henry He, Chris Love, Raj Beniwal, Praveen Kumar";            
  4. // Get a substring between two strings     
  5. int firstStringPosition = authors.IndexOf("Henry");    
  6. int secondStringPosition = authors.IndexOf("Beniwal");    
  7. string stringBetweenTwoStrings = authors.Substring(firstStringPosition,     
  8.     secondStringPosition - firstStringPosition + 7);    
  9. Console.WriteLine(stringBetweenTwoStrings);
The substring includes both strings as well. If you want to exclude the two strings, ‘Henry’ and ‘Beniwal’ from the substring, you can increase the starting position and reduce the length parameters in the Substring method.

7. Split a string into substrings separated by a character in C#

We can use String. Split method to separate a string into an array of strings. You can also further search for a character in a string and retrieve a substring from it.
The following code snippet splits a string into an array of substrings that are separated by a comma. After that, the Substring method is further used to retrieve substrings followed by ‘:’ from the substring.
  1. // Extract value of each attribute    
  2. string author = "Name: Mahesh Chand, Book: C# Programming, Publisher: C# Corner, Year: 2020";    
  3. string[] authorInfo = author.Split(", ");    
  4. foreach (string info in authorInfo)    
  5. {    
  6.     Console.WriteLine("   {0}", info.Substring(info.IndexOf(": ") + 1));    
  7. }


In this article and code sample, we learned about substrings in C# and how to use String.Substring method to retrieve various type of substrings.