简体   繁体   中英

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"


  • I cannot use expressions because the pattern changes every time.
  • The string.replace will not work, because you would need to hard code the values
  • Reading values in between double quotes does not work, because it gets thrown off by the quote in the middle

Please help...

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.

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"

我要做的是,查找", 。如果引号不在逗号旁边,请删除引号。

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()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM