C#字符串解析

时间:2017-02-03 23:12:40

标签: c#

我有这样的字符串

string temp = "'ADDR_LINE_2','MODEL','TABLE',5,'S','Y','C40','MUL,NBLD,NITA,NUND','','Address line 2'"

每对单引号都是以逗号分隔的字段。我想清空字符串中的第8个字段。我不能简单地做replace("MUL,NBLD,NITA,NUND",""),因为该字段可以包含任何内容。另请注意,第4个字段是一个数字,因此在5附近没有单引号。

我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:4)

static void Main()
{
    var temp = "'ADDR_LINE_2','MODEL','TABLE',5,'S','Y','C40','MUL,NBLD,NITA,NUND','','Address line 2'";

    var parts = Split(temp).ToArray();
    parts[7] = null;
    var ret = string.Join(",", parts);

    // or replace the above 3 lines with this...        
    //var ret = string.Join(",", Split(temp).Select((v,i)=>i!=7 ? v : null));

    //ret == "'ADDR_LINE_2','MODEL','TABLE',5,'S','Y','C40',,'','Address line 2'"
}

public static IEnumerable<string> Split(string input, char delimiter = ',', char quote = '\'')
{
    string temp = "";
    bool skipDelimiter = false;

    foreach (var c in input)
    {
        if (c == quote)
            skipDelimiter = !skipDelimiter;
        else if (c == delimiter && !skipDelimiter)
        {
            //do split
            yield return temp;
            temp = "";
            continue;
        }

        temp += c;
    }
    yield return temp;
}

答案 1 :(得分:1)

我在下面做了一个小实现。我在评论中解释逻辑。基本上你想写一个简单的解析器来完成你描述的。

编辑0:刚刚意识到我做了与你要求的oops相关的内容......现在修复

edit1:将字符串替换为null,而不是从逗号分隔列表中删除整个字段。

    static void Main(string[] args)
    {
        string temp = "'ADDR_LINE_2','MODEL','TABLE',5,'S','Y','C40','MUL,NBLD,NITA,NUND','','Address line 2'";
        //keep track of the single quotes
        int singleQuoteCount= 0;
        //keep track of commas
        int comma_count = 0;
        String field = "";
        foreach (Char chr in temp)
        {
            //add to the field string if we are not between the 7th and 8th comma not counting commas between single quotes
            if (comma_count != 7)
                field += chr;
            //plug in null string between two single quotes instead of whatever chars are in the eigth field.
            else if (chr == '\'' && singleQuoteCount %2 ==1)
                field += "\'',";

            if (chr == '\'') singleQuoteCount++;
            //only want to add to comma_count if we are outside of single quotes.
            if (singleQuoteCount % 2 == 0 && chr == ',') comma_count++;

        }
    }

答案 2 :(得分:-1)

如果您使用&#39; - &#39; (或其他字符)而不是&#39;,&#39;在字段内(考试:&#39; MUL-NBLD-NITA-NUND&#39;),您可以使用此代码:

    static void Main(string[] args)
    {
        string temp = "'ADDR_LINE_2','MODEL','TABLE',5,'S','Y','C40','MUL-NBLD-NITA-NUND','','Address line 2'";
        temp = replaceField(temp, 8);
    }

    static string replaceField(string list, int field)
    {
        string[] fields = list.Split(',');
        string chosenField = fields[field - 1 /*<--Arrays start at 0!*/];

        if(!(field == fields.Length))
            list = list.Replace(chosenField + ",", "");
        else
            list = list.Replace("," + chosenField, "");

        return list;
    }

    //Return-Value: "'ADDR_LINE_2','MODEL','TABLE',5,'S','Y','C40','','Address line 2'"