Shawn
Shawn

Reputation: 5260

C# string parse

I have string like this

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

Each pair of single quote is a field delimited by a comma. I want to empty the 8th field in the string. I cannot simply do replace("MUL,NBLD,NITA,NUND","") because that field could contain anything. also please note the the 4th field is a number and therefore has no single quote around 5.

How can I achieve this?

Upvotes: 1

Views: 143

Answers (3)

Luke Kot-Zaniewski
Luke Kot-Zaniewski

Reputation: 1161

I made a small implementation below. I explain the logic in the comments. Basically you want to write a simple parser to accomplish what you described.

edit0: just realized I did the opposite of what you asked for oops..fixed now

edit1: replacing the string with null as opposed to eliminating the entire field from the comma-delimited list.

    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++;

        }
    }

Upvotes: 1

FreaX
FreaX

Reputation: 94

If you would use '-' (or other char) instead of ',' inside of the fields (exam: 'MUL-NBLD-NITA-NUND'), you could use this code:

    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'"

Upvotes: -1

Matthew Whited
Matthew Whited

Reputation: 22443

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;
}

Upvotes: 4

Related Questions