A Sub-string Extractor with Specific Keywords Parameter Implementation in C# I am trying to implement a sub-string extractor with &quot;start keyword&quot; and &quot;end keyword&quot; and the extracted result is from (but excluded) the given start keyword to (but excluded) end keyword. For example:




Input String
Start Keyword
End Keyword
Output




&quot;C# was developed around 2000 by Microsoft as part of its .NET initiative&quot;
&quot;&quot;(empty string)
&quot;&quot;(empty string)
&quot;C# was developed around 2000 by Microsoft as part of its .NET initiative&quot;


&quot;C# was developed around 2000 by Microsoft as part of its .NET initiative&quot;
&quot;&quot;(empty string)
&quot;.NET&quot;
&quot;C# was developed around 2000 by Microsoft as part of its&quot;


&quot;C# was developed around 2000 by Microsoft as part of its .NET initiative&quot;
&quot;C#&quot;
&quot;&quot;(empty string)
&quot;was developed around 2000 by Microsoft as part of its .NET initiative&quot;


&quot;C# was developed around 2000 by Microsoft as part of its .NET initiative&quot;
&quot;C#&quot;
&quot;.NET&quot;
&quot;was developed around 2000 by Microsoft as part of its&quot;


&quot;C# was developed around 2000 by Microsoft as part of its .NET initiative&quot;
&quot;.NET&quot;
&quot;&quot;(empty string)
&quot;initiative&quot;


&quot;C# was developed around 2000 by Microsoft as part of its .NET initiative&quot;
&quot;&quot;(empty string)
&quot;C#&quot;
&quot;&quot;(empty string)


&quot;C# was developed around 2000 by Microsoft as part of its .NET initiative&quot;
&quot;.NET&quot;
&quot;C#&quot;
&quot;&quot;(empty string)




The experimental implementation
The experimental implementation is as below.
private static string GetTargetString(string stringInput, string startKeywordInput, string endKeywordInput)
{
    int startIndex;
    if (String.IsNullOrEmpty(startKeywordInput))
    {
        startIndex = 0;
    }
    else 
    {
        if (stringInput.IndexOf(startKeywordInput) &gt;= 0)
        {
            startIndex = stringInput.IndexOf(startKeywordInput) + startKeywordInput.Length;
        }
        else
        {
            return &quot;&quot;;
        }
        
    }

    int endIndex;
    if (String.IsNullOrEmpty(endKeywordInput))
    {
        endIndex = stringInput.Length;
    }
    else
    {
        if (stringInput.IndexOf(endKeywordInput) &gt; startIndex)
        {
            endIndex = stringInput.IndexOf(endKeywordInput);
        }
        else
        {
            return &quot;&quot;;
        }
    }
    
    
    //    Check startIndex and endIndex
    if (startIndex &lt; 0 || endIndex &lt; 0 || startIndex &gt;= endIndex)
    {
        return &quot;&quot;;
    }

    if (endIndex.Equals(0).Equals(true))
    {
        endIndex = stringInput.Length;
    }
    int TargetStringLength = endIndex - startIndex;
    return stringInput.Substring(startIndex, TargetStringLength).Trim();
}

Test cases
string test_string1 = &quot;C# was developed around 2000 by Microsoft as part of its .NET initiative&quot;;

Console.WriteLine(GetTargetString(test_string1, &quot;&quot;, &quot;&quot;));
Console.WriteLine(GetTargetString(test_string1, &quot;&quot;, &quot;.NET&quot;));
Console.WriteLine(GetTargetString(test_string1, &quot;C#&quot;, &quot;&quot;));
Console.WriteLine(GetTargetString(test_string1, &quot;C#&quot;, &quot;.NET&quot;));
Console.WriteLine(GetTargetString(test_string1, &quot;.NET&quot;, &quot;&quot;));
Console.WriteLine(GetTargetString(test_string1, &quot;&quot;, &quot;C#&quot;));
Console.WriteLine(GetTargetString(test_string1, &quot;.NET&quot;, &quot;C#&quot;));

The output of the above test cases.
C# was developed around 2000 by Microsoft as part of its .NET initiative
C# was developed around 2000 by Microsoft as part of its
was developed around 2000 by Microsoft as part of its .NET initiative
was developed around 2000 by Microsoft as part of its
initiative



If there is any possible improvement, please let me know.
I tend to have the &quot;error handling&quot; code at the beginning of the method, which usually makes the rest of the method more simple.
private static string GetTargetString(string input, string startKeyword, string endKeyword, StringComparison comparer)
{
    if (!string.IsNullOrEmpty(startKeyword) &amp;&amp; input.IndexOf(startKeyword, comparer) &lt; 0) return &quot;&quot;;
    if (!string.IsNullOrEmpty(endKeyword) &amp;&amp; input.IndexOf(endKeyword, comparer) &lt; 0) return &quot;&quot;;

    var startIndex = string.IsNullOrEmpty(startKeyword)
        ? 0
        : input.IndexOf(startKeyword, comparer) + startKeyword.Length;

    var endIndex = string.IsNullOrEmpty(endKeyword) 
        ? input.Length 
        : input.IndexOf(endKeyword, comparer);

    if (startIndex &lt; 0 || endIndex &lt; 0 || startIndex &gt;= endIndex) return &quot;&quot;;

    return input.Substring(startIndex, endIndex - startIndex).Trim();
}