Substring in C#
A substring 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.
- How to get a substring from a string?
- How to get a substring of the first n characters from a string?
- How to get a substring after a character?
- How to get a substring before a character?
- How to get a substring with a start and end index?
- How to get a substring from index to end?
- How to get a substring between two strings?
- How do I get the last 4 characters of a string in C#?
1. String.Substring method to get a substring of a string in C#
In C#, 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.
- Substring(Int32) - Retrieves a substring from the specified position to the end of the string.
- Substring(Int32, Int32 - Retrieves a substring from this instance from the specified position for the specified length.
Here's an example of using the Substring method in C#:
string str = "Hello World";
string substr1 = str.Substring(0, 5); // substr1 will contain "Hello"
string substr2 = str.Substring(6); // substr2 will contain "World"
In this example, we declare a string variable str containing the "Hello World" value. We then use the Substring method to extract two substrings from the original string.
The first substring is extracted using the overload of Substring that takes two arguments. The first argument is the starting index of the substring (in this case, 0), and the second argument is the length of the substring (in this case, 5). This means that the substr1 variable will contain the first 5 characters of the str string, which is "Hello".
The second substring is extracted using the overload of Substring that takes only one argument. In this case, we pass in the starting index of the substring (which is 6). This means that the substr2 variable will contain the remaining characters of the str string after the first space character, which is "World".
2. Get the first n characters substring from a string in C#
String characters are zero-indexed. This means the position of the first characters in a string starts at the 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.
// A long string
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.";
// Get first 12 characters substring from a string
string authorName = bio.Substring(0, 12);
Console.WriteLine(authorName);
Now, if you want to retrieve a substring, everything after the 12th position, you can simply pass the starting position in the string to get the rest of the string as a substring. For example, the following code snippet gets a substring that has all characters after the 12th position in a string.
// Get everything else after 12th position
string authorBio = bio.Substring(12);
Console.WriteLine(authorBio);
The complete code example is listed in Listing 1.
using System;
class Program
{
static void Main(string[] args)
{
// A long string
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.";
// Get first 12 characters substring from a string
string authorName = bio.Substring(0, 12);
Console.WriteLine(authorName);
// Get everything else after 12th position
string authorBio = bio.Substring(12);
Console.WriteLine(authorBio);
Console.ReadKey();
}
}
Listing 1.
The output of Listing 1 looks like Figure 1.



Md Masum KaziPosted May 28, 2025, 9:16 AM
Well explained
Aman GuptaPosted Jul 15, 2020, 9:44 PM
Intresting to read and useful.
Christian OforiPosted Jun 12, 2020, 10:25 AM
Nice Article, but how can i store each in a separate string from the loop. string name = "Mahesh Chand"; string book = "C# Programming"; string publisher = "C# Corner"; string year = "2020";
Sanghdeep SanghratnePosted Feb 26, 2020, 12:16 PM
Nice and useful article thanks to share with us
Rushi MehtaPosted Feb 26, 2020, 2:05 AM
Nice Article
Raju PaladiyaPosted Feb 25, 2020, 4:38 PM
Thank you for sharing this
Manoj KallaPosted Jan 6, 2020, 5:08 AM
Good article very helpful
Pankajkumar PatelPosted Aug 16, 2019, 1:01 AM
Nice article
Raju BaladaniyaPosted Dec 10, 2018, 1:44 AM
Good article
Marco Dalla LiberaPosted Sep 22, 2018, 2:12 AM
Many times instead of substring i prefer to use new string (value.Skip (skip) .Take (take) .ToArray ())
Mahesh ChandPosted Sep 16, 2018, 7:30 PM
Article updated
Md. Raskinur RashidPosted Jan 21, 2015, 3:10 AM
Thanks.
Dinesh RainaPosted Jan 17, 2015, 3:24 AM
pls help me, how to get a substring startposition and length from sql server.
Dinesh RainaPosted Jan 17, 2015, 3:23 AM
how to get a substring startposition and lenght from sql server.