Internet Engineer
Internet Engineer

Reputation: 2534

How to remove double quotes inside double quotes

I have a problem and cannot figure out what to do.

Question: I need to remove a double quote, inside double quotes


String example:

"MIKE YANICK","412 A AVE "E"," ","NADIEN PA"," ","190445468"

As you can see, the letter E inside "412 A AVE "E" has an extra double quote.


I need to remove it.

Here is what my outcome should be:

"MIKE YANICK","412 A AVE E"," ","NADIEN PA"," ","190445468"


Please help...

Upvotes: 4

Views: 2729

Answers (4)

Pedrom
Pedrom

Reputation: 3823

What about just using replace?

Something like:

string test = "\"MIKE YANICK\",\"412 A AVE \"E\",\" \",\"NADIEN PA\",\" \",\"190445468\"";
string[] fields = test.Split(new char[] {','});
StringBuilder result = new StringBuilder();
bool first = true;

foreach (var field in fields)
{
    if(first)
    {
        first = false;
    }
    else
    {
            result.Append(",");
    }

    result.AppendFormat("\"{0}\"", field.Replace("\"",""));
}

Console.WriteLine (string.Format("Old : {0} New: {1}", test,result.ToString()));

Upvotes: 0

BlackBear
BlackBear

Reputation: 22979

This works with your example:

Regex.Replace("\"MIKE YANICK\",\"412 A AVE \"E\",\" \",\"NADIEN PA\",\" \",\"190445468\"",
    "(?<=\")([^,]*)(?=\")",
    m => m.Value.Replace("\"", string.Empty)) ;

Output:

"MIKE YANICK","412 A AVE E"," ","NADIEN PA"," ","190445468"

Upvotes: 3

DJ Burb
DJ Burb

Reputation: 2364

What I would do is, look for ", . If the quotes are not next to a comma, delete the quotes.

Upvotes: 0

Matt Burland
Matt Burland

Reputation: 45135

You could use a regular expression like this:

(?<!(^|,))"(?!(,|$))

This will match any double quote (") that isn't proceeded by the start of the string, or a comma, and isn't followed by a comma or the end of the string.

Upvotes: 5

Related Questions