Monday, August 1, 2022

Get the text between two string with C#

 


public static string getBetween(string strSource, string strStart, string strEnd)

{

    if (strSource.Contains(strStart) && strSource.Contains(strEnd))

    {

        int Start, End;

        Start = strSource.IndexOf(strStart, 0) + strStart.Length;

        End = strSource.IndexOf(strEnd, Start);

        return strSource.Substring(Start, End - Start);

    }


    return "";

}


string source = "This is an example string and my data is here";

string data = getBetween(source, "my", "is");


Reference:

https://stackoverflow.com/questions/10709821/find-text-in-string-with-c-sharp

No comments:

Post a Comment