Example #1
0
        public static IEnumerable <string> SplitAndKeep(this string @string, char[] delims, StringSplitOptions options = StringSplitOptions.None)
        {
            var start = 0;
            var index = 0;

            while ((index = @string.IndexOfAny(delims, start)) != -1)
            {
                index = Interlocked.Exchange(ref start, index + 1);

                if (start - index - 1 > 0 || !options.HasFlag(StringSplitOptions.RemoveEmptyEntries))
                {
                    yield return(@string.Substring(index, start - index - 1));
                }

                yield return(@string.Substring(start - 1, 1));
            }

            if (options.HasFlag(StringSplitOptions.RemoveEmptyEntries))
            {
                if (start < @string.Length)
                {
                    yield return(@string.Substring(start));
                }
            }
            else
            {
                yield return(@string.Substring(start));
            }
        }
Example #2
0
        public static IEnumerable <string[]> GroupedLines(this string str, StringSplitOptions options = SplitOptions)
        {
            var buffer = new List <string>();

            foreach (var line in str.Lines(StringSplitOptions.None))
            {
                if (string.IsNullOrWhiteSpace(line))
                {
                    if (buffer.Any())
                    {
                        yield return(buffer.ToArray());

                        buffer.Clear();
                    }
                    else if (!options.HasFlag(StringSplitOptions.RemoveEmptyEntries))
                    {
                        yield return(Array.Empty <string>());
                    }
                }
                else
                {
                    buffer.Add(options.HasFlag(StringSplitOptions.TrimEntries) ? line.Trim() : line);
                }
            }
            if (buffer.Any())
            {
                yield return(buffer.ToArray());
            }
        }
Example #3
0
        public static IEnumerable <string> SplitBy(this string value, string delimiter,
                                                   StringSplitOptions options = StringSplitOptions.RemoveEmptyEntries)
        {
            var splitValues = value.Split(new[] { delimiter }, System.StringSplitOptions.None);

            if (options.HasFlag(StringSplitOptions.TrimWhiteSpaceFromEntries) ||
                options.HasFlag(StringSplitOptions.TrimWhiteSpaceAndRemoveEmptyEntries))
            {
                splitValues = splitValues.Select(s => s.Trim()).ToArray();
            }

            if (options.HasFlag(StringSplitOptions.RemoveEmptyEntries) ||
                options.HasFlag(StringSplitOptions.TrimWhiteSpaceAndRemoveEmptyEntries))
            {
                splitValues = splitValues.Where(s => !s.IsNullOrEmpty()).ToArray();
            }

            return(splitValues);
        }
Example #4
0
        public static IList <string> SplitQuoted(
            this string value,
            char delimiter,
            char quoteChar,
            char escapeChar,
            StringSplitOptions options)
        {
            // Remove the escaped and quoted characters, since we don't want to split on those, but leave
            // equal length replacements behind so the indexes line up.
            string stranged = new Regex($@"\{escapeChar}.").Replace(value, $"{escapeChar}\0");

            stranged =
                new Regex($@"\{quoteChar}([^\{quoteChar}]|\{escapeChar}.)*\{quoteChar}").Replace(
                    stranged,
                    m => new string('\0', m.Length));
            Debug.Assert(stranged.Length == value.Length);
            var parts    = new List <string>();
            int oldIndex = -1;
            int index    = -1;

            // use the indext from the mangled string...
            while ((index = stranged.IndexOf(delimiter, index + 1)) != -1)
            {
                // but substring the original (to restore the masked out characters)
                string part = value.Substring(oldIndex + 1, index - oldIndex - 1);
                if (part.Length > 0 || !options.HasFlag(StringSplitOptions.RemoveEmptyEntries))
                {
                    parts.Add(part);
                }

                oldIndex = index;
            }

            {
                string part = value.Substring(oldIndex + 1);
                if (part.Length > 0 || !options.HasFlag(StringSplitOptions.RemoveEmptyEntries))
                {
                    parts.Add(part);
                }
            }

            return(parts);
        }
Example #5
0
        public static IEnumerable <ReadOnlyMemory <char> > SplitSpan(this string str, char separator, StringSplitOptions options = StringSplitOptions.None)
        {
            var start = 0;

            for (var i = 0; i < str.Length; i++)
            {
                if (str[i] == separator)
                {
                    var length = i - start;
                    if (!options.HasFlag(StringSplitOptions.RemoveEmptyEntries) || length > 0)
                    {
                        yield return(str.AsMemory(start, i - start));
                    }
                    start = i + 1;
                }
            }

            if (start != str.Length)
            {
                yield return(str.AsMemory(start, str.Length - start));
            }
        }
Example #6
0
        public static IEnumerable <string> Split(this string sequence, string separator, StringComparison comparisonType, StringSplitOptions options)
        {
            var element = default(string);

            for (int a = 0, b = sequence.Length, c = separator.Length, d = 0; a < b; a = d)
            {
                d = sequence.IndexOf(separator, a, comparisonType);
                if (d == -1)
                {
                    element = sequence.Substring(a, b - a);
                    d       = b;
                }
                else
                {
                    element = sequence.Substring(a, d - a);
                    d      += c;
                }
                if (options.HasFlag(StringSplitOptions.RemoveEmptyEntries) && string.IsNullOrWhiteSpace(element))
                {
                    continue;
                }
                yield return(element.Trim());
            }
        }