public static string Substring(this string str, string startString, string endString, StringInclusionOptions stringInclusionOptions) { tryArgumentOutOfRangeException(str, startString, nameof(startString)); int startStringIndex = str.IndexOf(startString); tryArgumentOutOfRangeException(str.Substring(startStringIndex + startString.Length), endString, nameof(endString)); int endStringIndex = str.IndexOf(endString, startStringIndex + startString.Length); switch (stringInclusionOptions) { case StringInclusionOptions.IncludeNone: startStringIndex += startString.Length; break; case StringInclusionOptions.IncludeStart: break; case StringInclusionOptions.IncludeEnd: startStringIndex += startString.Length; endStringIndex += endString.Length; break; case StringInclusionOptions.IncludeAll: endStringIndex += endString.Length; break; } int selectLength = endStringIndex - startStringIndex; return(str.Substring(startStringIndex, selectLength)); }
public static string SubstringLast(this string str, string startString, string endString, StringInclusionOptions stringInclusionOptions = StringInclusionOptions.IncludeNone, StringComparison stringComparison = StringComparison.CurrentCulture) { Validator.CheckNullArgument(startString, nameof(startString)); Validator.CheckNullArgument(endString, nameof(endString)); Validator.CheckEndStringLastIndex(str, startString, endString, out int startStringIndex, out int endStringIndex, stringComparison); // This logic is because of how the "endStringIndex" is being calculated. switch (stringInclusionOptions) { case StringInclusionOptions.IncludeNone: startStringIndex += startString.Length; break; case StringInclusionOptions.IncludeStart: endStringIndex += startString.Length; break; case StringInclusionOptions.IncludeEnd: startStringIndex += startString.Length; endStringIndex += endString.Length; break; case StringInclusionOptions.IncludeAll: endStringIndex += endString.Length + startString.Length; break; } int selectLength = endStringIndex; return(str.Substring(startStringIndex, selectLength)); }
/// <summary> /// Retrieves a substring from this instance that is located between given start and end strings. /// </summary> /// <param name="str">The instance from which to extract a substring.</param> /// <param name="startString">The string which marks the start of the substring to be extracted.</param> /// <param name="endString">The string which marks the end of the substring.</param> /// <param name="stringInclusionOptions">A StringInclusionOptions enum, indicating whether startString and/or endString should be included in the result.</param> /// <returns>A string representing the part of the original string, located between the startString and endString.</returns> public static string Substring(this string str, string startString, string endString, StringInclusionOptions stringInclusionOptions) { // These are precaution calls, meant to throw an "ArgumentOutOfRangeException" exception if needed. str.Substring(str.IndexOf(startString)); str.Substring(str.IndexOf(endString)); int startStringIndex = str.IndexOf(startString); int endStringIndex = str.IndexOf(endString); switch (stringInclusionOptions) { case StringInclusionOptions.IncludeNone: startStringIndex += startString.Length; break; case StringInclusionOptions.IncludeStart: break; case StringInclusionOptions.IncludeEnd: startStringIndex += startString.Length; endStringIndex += endString.Length; break; case StringInclusionOptions.IncludeAll: endStringIndex += endString.Length; break; } int selectLength = endStringIndex - startStringIndex; return(str.Substring(startStringIndex, selectLength)); }