Skip to content

Instantly share code, notes, and snippets.

@viniciusmelocodes
Created August 5, 2020 17:34
Show Gist options
  • Save viniciusmelocodes/3de7350fc8d7b19b4f64913688c00214 to your computer and use it in GitHub Desktop.
Save viniciusmelocodes/3de7350fc8d7b19b4f64913688c00214 to your computer and use it in GitHub Desktop.
/// <summary>
/// A string extension method that get the string between the two specified string.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="before">The string before to search.</param>
/// <param name="after">The string after to search.</param>
/// <returns>The string between the two specified string.</returns>
public static string GetBetween(this string @this, string before, string after)
{
int beforeStartIndex = @this.IndexOf(before);
int startIndex = beforeStartIndex + before.Length;
int afterStartIndex = @this.IndexOf(after, startIndex);
if (beforeStartIndex == -1 || afterStartIndex == -1)
{
return "";
}
return @this.Substring(startIndex, afterStartIndex - startIndex);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment