示例#1
0
        /// <summary>
        ///     Gets the part of the input string between the before and after value, starting at the given start index,
        ///     and ending after the specified number of characters.
        /// </summary>
        /// <exception cref="ArgumentNullException">The string can not be null.</exception>
        /// <exception cref="ArgumentNullException">value can not be null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">The specified range is invalid.</exception>
        /// <param name="str">The input string.</param>
        /// <param name="before">The before value.</param>
        /// <param name="after">The after value.</param>
        /// <param name="startIndex">The start index of the string.</param>
        /// <param name="length">The length of the string, from the start index.</param>
        /// <returns>The part of the string between the before and after value.</returns>
        public static String GetBetween( this String str, Char before, Char after, Int32 startIndex, Int32 length )
        {
            // ReSharper disable once AccessToModifiedClosure
            str.ThrowIfNull( nameof( str ) );
            before.ThrowIfNull( nameof( before ) );
            after.ThrowIfNull( nameof( after ) );

            if ( startIndex < 0 || length < 0 || startIndex + length > str.Length )
                throw new ArgumentOutOfRangeException( "length", "The specified range is invalid." );

            str = str.Substring( startIndex, length );

            var beforeIndex = str.IndexOf( before );
            if ( beforeIndex < 0 )
                return String.Empty;

            var actualStartIndex = beforeIndex + 1;
            var afterIndex = str.IndexOf( after, actualStartIndex );
            return afterIndex < 0
                ? String.Empty
                : str.Substring( actualStartIndex, afterIndex - actualStartIndex );
        }