A part of a String is called a substring. In other words, a substring is a subset of another String. As you can see in the image house is a string and “use” is a substring of the house. In this short tutorial, we will discuss multiple ways of extracting substring from string.

How to extract substring in C#

Every language has its own inbuilt functions which can be used for finding the substring of any given string. In this article, we are taking an example of C#.

Find substring using the “Substring” method

C# has an inbuilt extension method for string datatype which can be used for extracting the substring of any string if you know the starting index and the number of characters you want to read.

The substring method has two overloads, we can extract the substring by using starting index only and also with starting index and length of the substring.

Substring from String using starting index in C#

In this case, we know about the starting index of the substring which we want to read. So, it will read the entire string after the provided index. Considering our example value, if we pass 2 as starting index then we should get “use” as output.

using System;
public class HelloWorld
{
    public static void Main(string[] args)
    {
        var house="House";
        Console.WriteLine(house.Substring(2));
    }
}
use

Substring using starting index and length in C#

considering the above example, we want to read the “us” out of house world. For that, we know the starting index is 2(because in the programming world we start with 0 not with 1, especially when you are dealing with arrays). And, the number of characters we want to read after that index is two. So lets try and see the result.

using System;
public class HelloWorld
{
    public static void Main(string[] args)
    {
        var house="House";
        Console.WriteLine(house.Substring(2,2));
    }
}
us

Find substring using loop in C#

If you know the starting index of the string and the number of characters of the substring. we can easily iterate through and find the required substring.

using System;
public class HelloWorld
{
    public static void Main(string[] args)
    {
        var house="House";
        var charArray=house.ToCharArray();//Convert string to array of characters
        var substring=string.Empty;
        var startIndex=2;
        var substringLength=3;
        for(int i=startIndex;i<(startIndex+substringLength);i++){
            substring+=charArray[i];
        }
        Console.WriteLine(substring);
    }
}
use

Find substring using indexof Method in C#

If you are not sure about the starting position of the substring inside the string, then we can utilize the indexof method to provide the starting index of the substring and continue from there.

eg: How are you doing? I am doing good.

Let says we want to read the remaining part of the string after the question mark.

using System;
public class HelloWorld
{
    public static void Main(string[] args)
    {
        var eg = "How are you doing? I am doing good.";
        var startIndex = eg.IndexOf('?')+1;
        Console.WriteLine(eg.Substring(startIndex));
    }
}
 I am doing good.

Next, the Scenario is to read the part of the string starting after the question mark and ending with the terminator sign. With the help of the “IndexOf” method, we can find the starting and ending index of the string we want to read out of above eg.

using System;
public class HelloWorld
{
    public static void Main(string[] args)
    {
        var eg = "How are you doing? I am doing good. something else";
        var startIndex = eg.IndexOf('?')+1;
        var endIndex = eg.IndexOf('.');
        var length=(endIndex-startIndex);
        Console.WriteLine(eg.Substring(startIndex, length));
    }
}
I am doing good

Conclusion

Finding the substring of any string is a very common operation that can be found in any project. And, every language has its own inbuilt methods which can be used to achieve this goal. In C# we have seen multiple ways of finding the substring of a provided string. We can use Substring if we know the index, and the IndexOf method if we don’t know the index of the substring. We can also achieve the same thing using for loop.

I know there are other ways to achieve the goal, Please do share if you know.

76 / 100