コード例 #1
0
        /// <summary>
        /// Determines if a substring of the current string instance
        /// starts with the target string (or a substring of this target string) specified by <paramref name="target" />.
        /// </summary>
        /// <param name="source">This string instance.</param>
        /// <param name="startIndex">The start position of the substring of the current string instance.</param>
        /// <param name="target">The target string instance to compare.</param>
        /// <param name="targetStartIndex">The start position of the substring of the target string instance.</param>
        /// <param name="targetLength">The length of the substring of the target string specified by <paramref name="target" />.</param>
        /// <param name="comparisonType">One of the enumeration values that specifies the rules for the comparison.</param>
        /// <returns>
        ///   <c>true</c> if the specified substring starts with the target substring (or a substring of this target string); otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// Occurs when at least one of <paramref name="startIndex"/>, <paramref name="targetStartIndex"/> and <paramref name="targetLength"/> is assigned an invalid value.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">Occurs when the argument <paramref name="target"/> is <c>null</c>.</exception>
        public static bool StartsWith(this string source, int startIndex, string target, int targetStartIndex, int targetLength, StringComparison comparisonType)
        {
            if (targetLength < 0)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetNonNegativeArgumentRequiredMessage("targetLength"));
            }

            if (targetStartIndex < 0)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetNonNegativeArgumentRequiredMessage("targetStartIndex"));
            }

            var realTargetLen = target?.Length ?? 0;

            var targetEndIndex = targetStartIndex + targetLength;

            if (targetEndIndex > realTargetLen)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetArgumentOutOfRangeMessage("targetLength", 0, true, target.Length - targetStartIndex, true));
            }

            var sourceLen = source.Length;

            if (startIndex < 0 && startIndex >= sourceLen)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetArgumentOutOfRangeMessage("startIndex", 0, true, sourceLen, false));
            }
            sourceLen -= startIndex;
            return(sourceLen >= targetLength && string.Compare(source, startIndex, target, targetStartIndex, targetLength, comparisonType) == 0);
        }
コード例 #2
0
        /// <summary>
        /// Reports the index of the last character satisfying the specified predicate.
        /// The search starts at a specified character position and examines a specified number of character positions toward the beginning of this string instance.
        /// </summary>
        /// <param name="str">This string instance.</param>
        /// <param name="predicate">A function to test each character of the current string.</param>
        /// <param name="startIndex">The search starting position. The search proceeds from <paramref name="startIndex" /> toward the beginning of this instance.</param>
        /// <param name="count">The number of character positions to examine.</param>
        /// <returns>
        /// The zero-based index position of the last character in this instance satisfying the specified predicate.
        /// </returns>
        public static int LastIndexOf(this string str, Func <char, bool> predicate, int startIndex, int count)
        {
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetNonNegativeArgumentRequiredMessage("count"));
            }
            if (startIndex < 0)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetArgumentOutOfRangeMessage("startIndex", 0, true, str.Length - 1, true));
            }
            var endIndex = startIndex - count;

            if (endIndex < 0)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetArgumentOutOfRangeMessage("count", 0, true, startIndex, true));
            }

            for (var i = startIndex; i >= endIndex; --i)
            {
                if (predicate(str[i]))
                {
                    return(i);
                }
            }
            return(-1);
        }
コード例 #3
0
        /// <summary>
        /// Determines if a substring of the current string instance
        /// ends with the target string (or a substring of this target string) specified by <paramref name="target" />.
        /// </summary>
        /// <param name="source">This string instance.</param>
        /// <param name="endIndex">The end position of the substring of the current string instance.</param>
        /// <param name="target">The target string instance to compare.</param>
        /// <param name="targetStartIndex">The start position of the substring of the target string instance.</param>
        /// <param name="targetLength">The length of the substring of the target string specified by <paramref name="target" />.</param>
        /// <param name="comparisonType">One of the enumeration values that specifies the rules for the comparison.</param>
        /// <returns>
        ///   <c>true</c> if the specified substring ends with the target substring (or a substring of this target string); otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// Occurs when at least one of <paramref name="endIndex"/>, <paramref name="targetStartIndex"/> and <paramref name="targetLength"/> is assigned an invalid value.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">Occurs when the argument <paramref name="target"/> is <c>null</c>.</exception>
        public static bool EndsWith(this string source, int endIndex, string target, int targetStartIndex, int targetLength, StringComparison comparisonType)
        {
            if (targetLength < 0)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetNonNegativeArgumentRequiredMessage("targetLength"));
            }

            if (targetStartIndex < 0)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetNonNegativeArgumentRequiredMessage("targetStartIndex"));
            }

            var realTargetLen = target == null ? 0 : target.Length;

            var targetEndIndex = targetStartIndex + targetLength;

            if (targetEndIndex > realTargetLen)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetArgumentOutOfRangeMessage("targetLength", 0, true, target.Length - targetStartIndex, true));
            }

            var sourceLen = source.Length;

            if (endIndex < 0 && endIndex >= sourceLen)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetArgumentOutOfRangeMessage("startIndex", 0, true, sourceLen, false));
            }

            if (targetLength != 0 && targetLength != 1)
            {
                endIndex -= (targetLength - 1);
            }
            if (endIndex < 0)
            {
                return(false);
            }
            return(string.Compare(source, endIndex, target, targetStartIndex, targetLength, comparisonType) == 0);
        }