/// <summary> /// Reports the zero-based index and length of the first occurrence of the specified substring in the source string. /// </summary> /// <param name="source">The source string in which to search.</param> /// <param name="substring">The substring to seek.</param> /// <param name="searchIndex">The zero-based starting character position in <paramref name="source"/> to search from.</param> /// <param name="comparisonType">One of the enumeration values that specifies the rules for the search.</param> /// <param name="matchIndex"> /// When this method returns, contains the zero-based starting character position of the match, if found; /// or -1 if no match is found. /// If <paramref name="substring"/> is the empty string (<c>""</c>), the value will be <paramref name="searchIndex"/>. /// </param> /// <param name="matchLength"> /// When this method returns, contains the length (in characters) of the match, if found; /// or -1 if no match is found. /// If <paramref name="substring"/> is the empty string (<c>""</c>), the value will be 0. /// </param> /// <remarks> /// Refer to the remarks on the <see cref="Find(string, string, int, int, StringComparison, out int, out int)"/> overload. /// </remarks> public static void Find(this string source, string substring, int searchIndex, StringComparison comparisonType, out int matchIndex, out int matchLength) { ArgumentValidate.NotNull(source, nameof(source)); ArgumentValidate.NotNull(substring, nameof(substring)); ArgumentValidate.StringIndex(source, nameof(source), searchIndex, nameof(searchIndex)); ArgumentValidate.EnumDefined(comparisonType, nameof(comparisonType)); FindInner(source, substring, searchIndex, source.Length - searchIndex, comparisonType, out matchIndex, out matchLength); }
/// <summary> /// Reports the zero-based index and length of the first occurrence of the specified substring in the source string. /// </summary> /// <param name="source">The source string in which to search.</param> /// <param name="searchValue">The substring to seek.</param> /// <param name="searchIndex">The zero-based starting character position in <paramref name="source"/> to search from.</param> /// <param name="comparisonType">One of the enumeration values that specifies the rules for the search.</param> /// <param name="matchIndex"> /// When this method returns, contains the zero-based starting character position of the match, if found; /// or -1 if no match is found. /// If <paramref name="searchValue"/> is the empty string (<c>""</c>), /// the value will be <paramref name="searchIndex"/>. /// </param> /// <param name="matchLength"> /// When this method returns, contains the length (in characters) of the match, if found; /// or -1 if no match is found. /// If <paramref name="searchValue"/> is the empty string (<c>""</c>), the value will be 0. /// </param> /// <returns> /// <see langword="true"/> if a match for <paramref name="searchValue"/> is found in the source string; /// otherwise, <see langword="false"/>. /// </returns> /// <remarks> /// Refer to the remarks on the /// <see cref="Find(string, string, int, int, StringComparison, out int, out int)"/> overload. /// </remarks> public static bool Find(this string source, string searchValue, int searchIndex, StringComparison comparisonType, out int matchIndex, out int matchLength) { ArgumentValidate.NotNull(source, nameof(source)); ArgumentValidate.NotNull(searchValue, nameof(searchValue)); ArgumentValidate.StringIndex(source, nameof(source), searchIndex, nameof(searchIndex)); ArgumentValidate.EnumDefined(comparisonType, nameof(comparisonType)); return(FindInner(source, searchValue, searchIndex, source.Length - searchIndex, comparisonType, out matchIndex, out matchLength)); }
private static void StringIndexFail <TException>( string str, int idx, bool strFail = false, bool idxFail = false) where TException : ArgumentException { var exception = ExceptionAssert.Throws <TException>(() => ArgumentValidate.StringIndex(str, nameof(str), idx, nameof(idx))); string failParamName = strFail ? nameof(str) : idxFail?nameof(idx) : null; Assert.AreEqual(failParamName, exception.ParamName); }
public void StringIndex() { string str; int idx; ArgumentValidate.StringIndex("", nameof(str), 0, nameof(idx)); ArgumentValidate.StringIndex("abc", nameof(str), 0, nameof(idx)); ArgumentValidate.StringIndex("abc", nameof(str), 1, nameof(idx)); ArgumentValidate.StringIndex("abc", nameof(str), 2, nameof(idx)); ArgumentValidate.StringIndex("abc", nameof(str), 3, nameof(idx)); StringIndexFail <ArgumentNullException>(null, 0, strFail: true); StringIndexFail <ArgumentOutOfRangeException>("", 1, idxFail: true); StringIndexFail <ArgumentOutOfRangeException>("abc", 4, idxFail: true); }