From 0c097b620749b42385ad7bd177256b7d1e8a6c9b Mon Sep 17 00:00:00 2001 From: Josh Creek Date: Wed, 25 Aug 2021 23:09:17 +0100 Subject: [PATCH] feat(*): Add SubstringBetween method --- Creek.HelpfulExtensions/StringExtension.cs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) 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); + } } }