Пример #1
0
        /// <summary>
        ///     Tokenize the given <see cref="System.String" /> into a
        ///     <see cref="System.String" /> array.
        /// </summary>
        /// <remarks>
        ///     <p>
        ///         If <paramref name="target" /> is <see langword="null" />, returns an empty
        ///         <see cref="System.String" /> array.
        ///     </p>
        ///     <p>
        ///         If <paramref name="delimiters" /> is <see langword="null" /> or the empty
        ///         <see cref="System.String" />, returns a <see cref="System.String" /> array with one
        ///         element: <paramref name="target" /> itself.
        ///     </p>
        /// </remarks>
        /// <param name="target">The <see cref="System.String" /> to tokenize.</param>
        /// <param name="delimiters">
        ///     The delimiter characters, assembled as a <see cref="System.String" />.
        /// </param>
        /// <param name="trimTokens">
        ///     Trim the tokens via <see cref="System.String.Trim()" />.
        /// </param>
        /// <param name="ignoreEmptyTokens">
        ///     Omit empty tokens from the result array.
        /// </param>
        /// <param name="quoteChars">
        ///     Pairs of quote characters. <paramref name="delimiters" /> within a pair of quotes are ignored
        /// </param>
        /// <returns>An array of the tokens.</returns>
        public static string[] Split(
            string target, string delimiters, bool trimTokens,
            bool ignoreEmptyTokens, string quoteChars)
        {
            if (target == null)
            {
                return(new string[0]);
            }
            if (string.IsNullOrEmpty(delimiters))
            {
                return(new[] { target });
            }
            if (quoteChars == null)
            {
                quoteChars = string.Empty;
            }
            AssertUtils.IsTrue(quoteChars.Length % 2 == 0, "the number of quote characters must be even");

            var delimiterChars = delimiters.ToCharArray();

            // scan separator positions
            var delimiterPositions = new int[target.Length];
            var count = MakeDelimiterPositionList(target, delimiterChars, quoteChars, delimiterPositions);

            var tokens     = new List <string>(count + 1);
            var startIndex = 0;

            for (var ixSep = 0; ixSep < count; ixSep++)
            {
                var token = target.Substring(startIndex, delimiterPositions[ixSep] - startIndex);
                if (trimTokens)
                {
                    token = token.Trim();
                }
                if (!(ignoreEmptyTokens && token.Length == 0))
                {
                    tokens.Add(token);
                }
                startIndex = delimiterPositions[ixSep] + 1;
            }
            // add remainder
            if (startIndex < target.Length)
            {
                var token = target.Substring(startIndex);
                if (trimTokens)
                {
                    token = token.Trim();
                }
                if (!(ignoreEmptyTokens && token.Length == 0))
                {
                    tokens.Add(token);
                }
            }
            else if (startIndex == target.Length)
            {
                if (!(ignoreEmptyTokens))
                {
                    tokens.Add(string.Empty);
                }
            }

            return(tokens.ToArray());
        }