diff --git a/Creek.HelpfulExtensions/StringExtension.cs b/Creek.HelpfulExtensions/StringExtension.cs index c742cb4..56f12b4 100644 --- a/Creek.HelpfulExtensions/StringExtension.cs +++ b/Creek.HelpfulExtensions/StringExtension.cs @@ -4,5 +4,27 @@ namespace Creek.HelpfulExtensions { public static class StringExtension { + /// + /// Returns a substring from the first occurrence of the start string to the end string. + /// + /// The string to find a substring from. + /// The string to begin the substring with. + /// The string that should occur immediately after the substring. + /// Allows selecting whether to use the first occurrence of the endString (default behaviour) or the last. + /// Returns the substring. + public static string SubstringBetween(this string str, string startString, string endString, bool isLastEndStringOccurrence = false) + { + // Find the start index of the startString + int startIndex = str.IndexOf(startString); + + // Find the end index of the endString + int endIndex = isLastEndStringOccurrence ? str.LastIndexOf(endString) : str.IndexOf(endString); + + // Find the length of the string we want + int length = endIndex - startIndex; + + // Substring(int startIndex, int length) + return str.Substring(startIndex, length); + } } }