如何使用封装和转义拆分分隔的字符串

时间:2009-03-11 16:18:17

标签: c#

我想分割一个由'&'等字符分隔的字符串,但是在某些值包含分隔符的情况下,我想用双引号转义。什么是优雅的分裂方法,同时忽略已经转义的转义字符,同时还要考虑转义字符转义?

例如正确拆分此字符串

var1=asdfasdf&var2=contain””quote&var3=”contain&delim”&var4=”contain””both&”

分为:

var1=asdfasdf
var2=contain"quote
var3=contain&delim
var4=contain"both&

顺便说一句,我在想Regex ......

1 个答案:

答案 0 :(得分:0)

我的解决方案,测试:

    void TestCharlesParse()
    {
        string s = @"var1=asdfasdf&var2=contain""""quote&var3=""contain&delim""&var4=""contain""""both&""";
        string[] os = CharlesParse(s);

        for (int i = 0; i < os.Length; i++)
            System.Windows.Forms.MessageBox.Show(os[i]);
    }

    string[] CharlesParse(string inputString)
    {
        string[] escapedQuotes = { "\"\"" };

        string[] sep1 = inputString.Split(escapedQuotes, StringSplitOptions.None);

        bool quoted = false;
        ArrayList sep2 = new ArrayList();
        for (int i = 0; i < sep1.Length; i++)
        {
            string[] sep3 = ((string)sep1[i]).Split('"');
            for (int j = 0; j < sep3.Length; j++)
            {
                if (!quoted)
                {
                    string[] sep4 = sep3[j].Split('&');
                    for (int k = 0; k < sep4.Length; k++)
                    {
                        if (k == 0)
                        {
                            if (sep2.Count == 0)
                            {
                                sep2.Add(sep4[k]);
                            }
                            else
                            {
                                sep2[sep2.Count - 1] = ((string)sep2[sep2.Count - 1]) + sep4[k];
                            }
                        }
                        else
                        {
                            sep2.Add(sep4[k]);
                        }
                    }
                }
                else
                {
                    sep2[sep2.Count - 1] = ((string)sep2[sep2.Count - 1]) + sep3[j];
                }
                if (j < (sep3.Length-1))
                    quoted = !quoted;
            }
            if (i < (sep1.Length - 1))
                sep2[sep2.Count - 1] = ((string)sep2[sep2.Count - 1]) + "\"";
        }

        string[] ret = new string[sep2.Count];
        for (int l = 0; l < sep2.Count; l++)
            ret[l] = (string)sep2[l];

        return ret;
    }