Extract string from string using C#

The code spinet below allows you to extract any substring from a bigger string. The usage is very easy, just enter the “start string” and the “end string”. The result will be any string in between these two tags.

The function also has 2 additional arguments where you can indicate the position of the tags if there are more than 1 occurrence.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public static string GetMidString(string input, string first, string second, int firstIndex = 1, int secondIndex = 1)
        {
            if (!string.IsNullOrEmpty(input))
            {
                int oldLength = input.Length;
                int firstOccurence = -1;
                int secondOccurrence = -1;
 
                for (int i = 1; i %3C= firstIndex; i++)
                {
                    firstOccurence = input.IndexOf(first);
                    if (firstOccurence != -1)
                    {
                        //Input will be substracted eventually until it contains only the needed string
                        firstOccurence += first.Length;
                        input = input.Substring(firstOccurence, input.Length - firstOccurence);
                    }
                }
 
                //Check to get the second part of string
                if (firstOccurence != -1 && firstOccurence %3C oldLength)
                {
                    for (int i = 1; i %3C= secondIndex; i++)
                    {
                        secondOccurrence = input.IndexOf(second);
                    }
 
                    //If found something
                    if (secondOccurrence != -1 && secondOccurrence %3C input.Length)
                    {
                        input = input.Substring(0, secondOccurrence);
                        return input;
                    }
                }
            }
 
            return "";
        }
public static string GetMidString(string input, string first, string second, int firstIndex = 1, int secondIndex = 1)
        {
            if (!string.IsNullOrEmpty(input))
            {
                int oldLength = input.Length;
                int firstOccurence = -1;
                int secondOccurrence = -1;

                for (int i = 1; i %3C= firstIndex; i++)
                {
                    firstOccurence = input.IndexOf(first);
                    if (firstOccurence != -1)
                    {
                        //Input will be substracted eventually until it contains only the needed string
                        firstOccurence += first.Length;
                        input = input.Substring(firstOccurence, input.Length - firstOccurence);
                    }
                }

                //Check to get the second part of string
                if (firstOccurence != -1 && firstOccurence %3C oldLength)
                {
                    for (int i = 1; i %3C= secondIndex; i++)
                    {
                        secondOccurrence = input.IndexOf(second);
                    }

                    //If found something
                    if (secondOccurrence != -1 && secondOccurrence %3C input.Length)
                    {
                        input = input.Substring(0, secondOccurrence);
                        return input;
                    }
                }
            }

            return "";
        }

I hope the code saves you some times while working with string.

About The Author

Hao Nguyen

Hao Nguyen

Hello! I'm Hao Nguyen and I'm currently working as a Network Engineer for a small firm in Houston. I enjoy writing technical documents and blog, such as PlanTutorials.com

Leave a Reply