/// <summary> /// Returns the left part from the first ocurrence of the given substring (without the substring). /// </summary> /// <remarks> /// Features driven by this set of rules: /// <list type="bullet"> /// <item><description>null.LeftFromFirst(*) --> null</description></item> /// <item><description>null and string.Empty are always substrings of a not null string</description></item> /// /// <item><description>string.Empty only contains itself and null</description></item> /// <item><description>*.LeftFromFirst(notFound) --> null</description></item> /// </list> /// </remarks> public static string LeftFromFirst(this SubstrExtensionPoint sp, string substring, StringComparison comparison = StringComparison.Ordinal) { string e = sp.ExtendedValue; return(e.Safe(s => { substring = substring.EmptyIfNull(); int indexOfSubstringStart = s.IndexOf(substring, comparison) >= 0 ? s.IndexOf(substring, comparison) : -1; return indexOfSubstringStart < 0 ? null : sp.Left(indexOfSubstringStart); })); }
/// <summary> /// Returns the left part from the last ocurrence of the given substring (without the substring). /// </summary> /// <remarks> /// Features driven by this set of rules: /// <list type="bullet"> /// <item><description>null.LeftFromLast(*) --> null</description></item> /// <item><description>null and string.Empty are always substrings of a not null string</description></item> /// /// <item><description>string.Empty only contains itself and null</description></item> /// <item><description>*.LeftFromLast(notFound) --> null</description></item> /// </list> /// </remarks> public static string LeftFromLast(this SubstrExtensionPoint sp, string substring, StringComparison comparison = StringComparison.Ordinal) { string e = sp.ExtendedValue; return(e.Safe(s => { substring = substring.EmptyIfNull(); int indexOfSubstringStart = -1; if (substring.IsEmpty()) { indexOfSubstringStart = 0; } else if (s.LastIndexOf(substring, comparison) >= 0) { indexOfSubstringStart = s.LastIndexOf(substring, comparison); } return indexOfSubstringStart < 0 ? null : sp.Left(indexOfSubstringStart); })); }