Ejemplo n.º 1
0
            /// <summary>
            /// returns a substring of specified <paramref name="str"/> in the range
            /// [<paramref name="startIndex"/>, <paramref name="endIndex"/>).
            /// </summary>
            /// <param name="str"></param>
            /// <param name="startIndex"></param>
            /// <param name="endIndex"></param>
            /// <returns>
            /// substring of specified <paramref name="str"/> in the range
            /// [<paramref name="startIndex"/>, <paramref name="endIndex"/>)
            /// </returns>
            /// <exception cref="IndexOutOfRangeException">
            /// <seealso cref="StringUtils.AssertValidRangeIndicesInString(int, int, string)"/>
            /// </exception>
            public static string SubstringByIndex(this string str, int startIndex, int endIndex)
            {
                StringUtils.AssertValidRangeIndicesInString(startIndex, endIndex, str);

                int substringLength = endIndex - startIndex;

                StringUtils.AssertValidStringLengths(substringLength);

                return(str.Substring(startIndex, substringLength));
            }
Ejemplo n.º 2
0
            /// <summary>
            /// returns a substring of <paramref name="str"/> in range
            /// [<paramref name="startIndex"/>, <paramref name="endIndex"/>),
            /// omitting all occurrences of any expression contained in specified
            /// <paramref name="expressionsToOmit"/>.
            /// </summary>
            /// <param name="str"></param>
            /// <param name="startIndex"></param>
            /// <param name="endIndex"></param>
            /// <param name="expressionsToOmit"></param>
            /// <returns>
            /// substring of <paramref name="str"/> in range
            /// [<paramref name="startIndex"/>, <paramref name="endIndex"/>),
            /// omitting all occurrences of any expression contained in specified
            /// <paramref name="expressionsToOmit"/>
            /// </returns>
            /// <exception cref="IndexOutOfRangeException">
            /// <seealso cref="StringUtils.AssertValidRangeIndicesInString(int, int, string)"/>
            /// </exception>
            /// <exception cref="ArgumentOutOfRangeException">
            /// <seealso cref="StringUtils.AssertValidStringLengths(int[])"/>
            /// </exception>
            public static string SubstringByIndexWithout(
                this string str,
                int startIndex,
                int endIndex,
                params string[] expressionsToOmit)
            {
                StringUtils.AssertValidRangeIndicesInString(startIndex, endIndex, str);
                StringUtils.AssertValidStringLengths(endIndex - startIndex);

                StringBuilder substringBuilder = new StringBuilder();

                // current index in str, starting at startIndex
                int curStringIndex = startIndex;

                // index of the first occurrence of any of the specified expressionsToOmit,
                // starting at curStringIndex
                int indexOfFirstOccurrenceOfChosenExpressionToOmit;

                // expression whose occurrence is the first to appear, starting at curStringIndex
                string chosenExpressionToOmit;

                // get index of first occurrence of any of the specified expressionsToOmit,
                // starting at beginning of str
                indexOfFirstOccurrenceOfChosenExpressionToOmit = str.IndexOfAny(
                    curStringIndex,
                    expressionsToOmit,
                    out chosenExpressionToOmit);

                // occurrence of an expression to omit, starting at index in range
                // [curStringIndex, endIndex), was found
                while (
                    indexOfFirstOccurrenceOfChosenExpressionToOmit != -1 &&
                    indexOfFirstOccurrenceOfChosenExpressionToOmit < endIndex)
                {
                    // append all characters from str, upto first occurrence of expressionToOmit,
                    // to substringBuilder
                    while (curStringIndex < indexOfFirstOccurrenceOfChosenExpressionToOmit)
                    {
                        substringBuilder.Append(str[curStringIndex]);
                        curStringIndex++;
                    }

                    // skip over expressionToOmit
                    curStringIndex += chosenExpressionToOmit.Length;

                    if (curStringIndex < endIndex) // endIndex not yet reached
                    {
                        // get index of first occurrence of any of the specified expressionsToOmit,
                        // starting at curStringIndex
                        indexOfFirstOccurrenceOfChosenExpressionToOmit = str.IndexOfAny(
                            curStringIndex,
                            expressionsToOmit,
                            out chosenExpressionToOmit);
                    }
                    else // endIndex reached - no next occurrence available
                    {
                        indexOfFirstOccurrenceOfChosenExpressionToOmit = -1;
                    }
                }

                // append the remainder of str (after last occurrence of an expression to omit)
                // to substringBuilder
                while (curStringIndex < endIndex)
                {
                    substringBuilder.Append(str[curStringIndex]);
                    curStringIndex++;
                }

                return(substringBuilder.ToString());
            }