Exemple #1
0
        public static string[] SplitNTrim(this string text, char[] Separators, ChoStringSplitOptions stringSplitOptions, char quoteChar = '\0')
        {
            if (text == null || text.Trim().Length == 0)
            {
                return new string[] { }
            }
            ;

            string        word;
            List <string> tokenList = new List <string>();

            foreach (string token in Split(text, Separators, stringSplitOptions, quoteChar))
            {
                word = token != null?token.Trim() : token;

                if (String.IsNullOrEmpty(word))
                {
                    if (stringSplitOptions != ChoStringSplitOptions.RemoveEmptyEntries)
                    {
                        tokenList.Add(word);
                    }
                }
                else
                {
                    tokenList.Add(word);
                }
            }

            return(tokenList.ToArray());
        }
Exemple #2
0
        public static string[] SplitNTrim(this string text, string separator, ChoStringSplitOptions stringSplitOptions, char quoteChar = '\0',
                                          char quoteEscapeChar = '\0')
        {
            if (text == null || text.Trim().Length == 0)
            {
                return new string[] { }
            }
            ;
            if (quoteEscapeChar == '\0')
            {
                quoteEscapeChar = quoteChar;
            }

            string        word;
            List <string> tokenList = new List <string>();

            foreach (string token in Split(text, (object)separator, stringSplitOptions, quoteChar, quoteEscapeChar))
            {
                word = token != null?NormalizeString(token.Trim(), quoteChar) : token;

                if (String.IsNullOrEmpty(word))
                {
                    if (stringSplitOptions != ChoStringSplitOptions.RemoveEmptyEntries)
                    {
                        tokenList.Add(word);
                    }
                }
                else
                {
                    tokenList.Add(word);
                }
            }

            return(tokenList.ToArray());
        }
Exemple #3
0
        private static string[] Split(this string text, object separators, ChoStringSplitOptions stringSplitOptions,
                                      char quoteChar = '\0', char quoteEscape = ChoCharEx.Backslash)
        {
            if (String.IsNullOrEmpty(text))
            {
                return(new string[0]);
            }

            if (separators is char[] && ((char[])separators).Length == 0)
            {
                throw new ApplicationException("Invalid separator characters passed.");
            }
            else if (separators is string && ((string)separators).Length == 0)
            {
                throw new ApplicationException("Invalid separator characters passed.");
            }

            if (quoteChar == '\0')
            {
                if (separators is char[])
                {
                    return(text.Split(separators as char[]));
                }
                else if (separators is string)
                {
                    return(text.Split(new string[] { (string)separators }, StringSplitOptions.None));
                }
                else
                {
                    throw new NotSupportedException();
                }
            }
            else if (separators is char[] && ((char[])separators).Length == 1)
            {
                return(text.FastSplit(((char[])separators)[0], quoteChar, quoteEscape));
            }
            else if (separators is string && ((string)separators).Length == 1)
            {
                return(text.FastSplit(((string)separators)[0], quoteChar, quoteEscape));
            }

            List <string> splitStrings = new List <string>();

            if (separators is char[] && Array.IndexOf(((char[])separators), quoteChar) >= 0)
            {
                throw new ApplicationException("Invalid separator characters passed.");
            }

            int    len          = separators is char[] ? 0 : ((string)separators).Length - 1;
            int    i            = 0;
            int    quotes       = 0;
            int    singleQuotes = 0;
            int    offset       = 0;
            bool   hasChar      = false;
            string word         = null;

            while (i < text.Length)
            {
                if ((stringSplitOptions & ChoStringSplitOptions.AllowQuotes) != ChoStringSplitOptions.AllowQuotes && text[i] == quoteChar)
                {
                    quotes++;
                }
                //else if ((stringSplitOptions & ChoStringSplitOptions.AllowSingleQuoteEntry) != ChoStringSplitOptions.AllowSingleQuoteEntry && text[i] == '\'') { singleQuotes++; }
                else if (text[i] == '\\'
                         /*&& i + 1 < text.Length && Contains(text, ++i, separators)*/)
                //hasChar = true;
                {
                    i++;
                }
                else if (Contains(text, i, separators) &&
                         ((quotes > 0 && quotes % 2 == 0) || (singleQuotes > 0 && singleQuotes % 2 == 0)) ||
                         Contains(text, i, separators) && quotes == 0 && singleQuotes == 0)
                {
                    if (hasChar)
                    {
                        string subString = offset == 0 ? text.Substring(offset, i) : text.Substring(offset, i - offset);
                        word = NormalizeString(subString.Replace("\\", String.Empty), quoteChar);
                        if (String.IsNullOrEmpty(word))
                        {
                            if (stringSplitOptions != ChoStringSplitOptions.RemoveEmptyEntries)
                            {
                                splitStrings.Add(word);
                            }
                        }
                        else
                        {
                            splitStrings.Add(word);
                        }

                        hasChar = false;
                    }
                    else
                    {
                        string subString = offset == 0 ? text.Substring(offset, i) : text.Substring(offset, i - offset);
                        //if (subString.Length == 2)
                        //    splitStrings.Add(subString);
                        //else
                        //{
                        word = NormalizeString(subString, quoteChar);
                        if (String.IsNullOrEmpty(word))
                        {
                            if (stringSplitOptions != ChoStringSplitOptions.RemoveEmptyEntries)
                            {
                                splitStrings.Add(word);
                            }
                        }
                        else
                        {
                            splitStrings.Add(word);
                        }
                        //}
                        i = i + len;
                    }

                    offset = i + 1;
                }
                i++;
            }

            if (offset <= text.Length)
            {
                splitStrings.Add(hasChar ? NormalizeString(text.Substring(offset).Replace("\\", String.Empty), quoteChar) :
                                 NormalizeString(text.Substring(offset), quoteChar));
            }

            return(splitStrings.ToArray());
        }
Exemple #4
0
        private static string[] Split(this string text, object Separators, ChoStringSplitOptions stringSplitOptions, char quoteChar = '"')
        {
            if (String.IsNullOrEmpty(text))
            {
                return(new string[0]);
            }

            List <string> splitStrings = new List <string>();

            if (quoteChar == '\0')
            {
                quoteChar = '"';
            }

            if (Separators is char[] && Array.IndexOf(((char[])Separators), quoteChar) >= 0)
            {
                throw new ApplicationException("Invalid quote character passed.");
            }
            else if (Separators is string && ((string)Separators).Contains(quoteChar))
            {
                throw new ApplicationException("Invalid quote character passed.");
            }

            int    len          = Separators is char[] ? 0 : ((string)Separators).Length - 1;
            int    i            = 0;
            int    quotes       = 0;
            int    singleQuotes = 0;
            int    offset       = 0;
            bool   hasChar      = false;
            string word         = null;

            while (i < text.Length)
            {
                if ((stringSplitOptions & ChoStringSplitOptions.AllowQuotes) != ChoStringSplitOptions.AllowQuotes && text[i] == quoteChar)
                {
                    quotes++;
                }
                //else if ((stringSplitOptions & ChoStringSplitOptions.AllowSingleQuoteEntry) != ChoStringSplitOptions.AllowSingleQuoteEntry && text[i] == '\'') { singleQuotes++; }
                else if (text[i] == '\\' &&
                         i + 1 < text.Length && Contains(text, ++i, Separators))
                {
                    hasChar = true;
                }
                else if (Contains(text, i, Separators) &&
                         ((quotes > 0 && quotes % 2 == 0) || (singleQuotes > 0 && singleQuotes % 2 == 0)) ||
                         Contains(text, i, Separators) && quotes == 0 && singleQuotes == 0)
                {
                    if (hasChar)
                    {
                        word = NormalizeString(text.Substring(offset, i - len - offset).Replace("\\", String.Empty), quoteChar);
                        if (String.IsNullOrEmpty(word))
                        {
                            if (stringSplitOptions != ChoStringSplitOptions.RemoveEmptyEntries)
                            {
                                splitStrings.Add(word);
                            }
                        }
                        else
                        {
                            splitStrings.Add(word);
                        }

                        hasChar = false;
                    }
                    else
                    {
                        string subString = text.Substring(offset, i - len - offset);
                        //if (subString.Length == 2)
                        //    splitStrings.Add(subString);
                        //else
                        //{
                        word = NormalizeString(subString, quoteChar);
                        if (String.IsNullOrEmpty(word))
                        {
                            if (stringSplitOptions != ChoStringSplitOptions.RemoveEmptyEntries)
                            {
                                splitStrings.Add(word);
                            }
                        }
                        else
                        {
                            splitStrings.Add(word);
                        }
                        //}
                    }

                    offset = i + 1;
                }
                i++;
            }

            splitStrings.Add(hasChar ? NormalizeString(text.Substring(offset).Replace("\\", String.Empty), quoteChar) :
                             NormalizeString(text.Substring(offset), quoteChar));

            return(splitStrings.ToArray());
        }
Exemple #5
0
 public static string[] Split(this string text, string value, ChoStringSplitOptions stringSplitOptions, char?quoteChar = '\0', char?quoteEscape = ChoCharEx.Backslash)
 {
     return(Split(text, (object)value, stringSplitOptions, (quoteChar == null ? '\0' : quoteChar.Value),
                  (quoteEscape == null ? ChoCharEx.Backslash : quoteEscape.Value)));
 }
Exemple #6
0
 public static string[] Split(this string text, string value, ChoStringSplitOptions stringSplitOptions, char quoteChar = '"')
 {
     return(Split(text, (object)value, stringSplitOptions, quoteChar));
 }
Exemple #7
0
 /// <summary>
 /// Split the string into multiple strings by the Separators.
 /// </summary>
 /// <param name="text">A string value to be split.</param>
 /// <param name="Separators">List of Separators used to split the string.</param>
 /// <param name="ignoreEmptyWord">true, to ignore the empry words in the output list</param>
 /// <returns>A string array contains splitted values, if the input text is null/empty, an empty array will be returned.</returns>
 public static string[] Split(this string text, char[] Separators, ChoStringSplitOptions stringSplitOptions, char quoteChar = '"')
 {
     return(Split(text, (object)Separators, stringSplitOptions, quoteChar));
 }
Exemple #8
0
        private static string[] Split(this string text, object Separators, ChoStringSplitOptions stringSplitOptions)
        {
            if (String.IsNullOrEmpty(text))
            {
                return(new string[0]);
            }

            List <string> splitStrings = new List <string>();

            int    len          = Separators is char[] ? 0 : ((string)Separators).Length - 1;
            int    i            = 0;
            int    quotes       = 0;
            int    singleQuotes = 0;
            int    offset       = 0;
            bool   hasChar      = false;
            string word         = null;

            while (i < text.Length)
            {
                if ((stringSplitOptions & ChoStringSplitOptions.AllowDoubleQuoteEntry) != ChoStringSplitOptions.AllowDoubleQuoteEntry && text[i] == '\"')
                {
                    quotes++;
                }
                else if ((stringSplitOptions & ChoStringSplitOptions.AllowSingleQuoteEntry) != ChoStringSplitOptions.AllowSingleQuoteEntry && text[i] == '\'')
                {
                    singleQuotes++;
                }
                else if (text[i] == '\\' &&
                         i + 1 < text.Length && Contains(text, ++i, Separators))
                {
                    hasChar = true;
                }
                else if (Contains(text, i, Separators) &&
                         ((quotes > 0 && quotes % 2 == 0) || (singleQuotes > 0 && singleQuotes % 2 == 0)) ||
                         Contains(text, i, Separators) && quotes == 0 && singleQuotes == 0)
                {
                    if (hasChar)
                    {
                        word = NormalizeString(text.Substring(offset, i - len - offset).Replace("\\", String.Empty));
                        if (String.IsNullOrEmpty(word))
                        {
                            if (stringSplitOptions != ChoStringSplitOptions.RemoveEmptyEntries)
                            {
                                splitStrings.Add(word);
                            }
                        }
                        else
                        {
                            splitStrings.Add(word);
                        }

                        hasChar = false;
                    }
                    else
                    {
                        string subString = text.Substring(offset, i - len - offset);
                        if (subString.Length == 2)
                        {
                            splitStrings.Add(subString);
                        }
                        else
                        {
                            word = NormalizeString(subString);
                            if (String.IsNullOrEmpty(word))
                            {
                                if (stringSplitOptions != ChoStringSplitOptions.RemoveEmptyEntries)
                                {
                                    splitStrings.Add(word);
                                }
                            }
                            else
                            {
                                splitStrings.Add(word);
                            }
                        }
                    }

                    offset = i + 1;
                }
                i++;
            }

            splitStrings.Add(hasChar ? NormalizeString(text.Substring(offset).Replace("\\", String.Empty)) : NormalizeString(text.Substring(offset)));

            return(splitStrings.ToArray());
        }
Exemple #9
0
 public static string[] Split(this string text, string value, ChoStringSplitOptions stringSplitOptions)
 {
     return(Split(text, (object)value, stringSplitOptions));
 }
Exemple #10
0
 /// <summary>
 /// Split the string into multiple strings by the Separators.
 /// </summary>
 /// <param name="text">A string value to be split.</param>
 /// <param name="Separators">List of Separators used to split the string.</param>
 /// <param name="ignoreEmptyWord">true, to ignore the empry words in the output list</param>
 /// <returns>A string array contains splitted values, if the input text is null/empty, an empty array will be returned.</returns>
 public static string[] Split(this string text, char[] Separators, ChoStringSplitOptions stringSplitOptions)
 {
     return(Split(text, (object)Separators, stringSplitOptions));
 }