简体   繁体   中英

How can I remove quoted string literals from a string in C#?

I have a string:

Hello "quoted string" and 'tricky"stuff' world

and want to get the string minus the quoted parts back. Eg,

Hello and world

Any suggestions?

resultString = Regex.Replace(subjectString, 
    @"([""'])# Match a quote, remember which one
    (?:      # Then...
     (?!\1)  # (as long as the next character is not the same quote as before)
     .       # match any character
    )*       # any number of times
    \1       # until the corresponding closing quote
    \s*      # plus optional whitespace
    ", 
    "", RegexOptions.IgnorePatternWhitespace);

will work on your example.

resultString = Regex.Replace(subjectString, 
    @"([""'])# Match a quote, remember which one
    (?:      # Then...
     (?!\1)  # (as long as the next character is not the same quote as before)
     \\?.    # match any escaped or unescaped character
    )*       # any number of times
    \1       # until the corresponding closing quote
    \s*      # plus optional whitespace
    ", 
    "", RegexOptions.IgnorePatternWhitespace);

will also handle escaped quotes.

So it will correctly transform

Hello "quoted \"string\\" and 'tricky"stuff' world

into

Hello and world

Use a regular expression to match any quoted strings with the string and replace them with the empty string. Use the Regex.Replace() method to do the pattern matching and replacement.

In case, like me, you're afraid of regex, I've put together a functional way to do it, based on your example string. There's probably a way to make the code shorter, but I haven't found it yet.

private static string RemoveQuotes(IEnumerable<char> input)
{
    string part = new string(input.TakeWhile(c => c != '"' && c != '\'').ToArray());
    var rest = input.SkipWhile(c => c != '"' && c != '\'');
    if(string.IsNullOrEmpty(new string(rest.ToArray())))
        return part;
    char delim = rest.First();
    var afterIgnore = rest.Skip(1).SkipWhile(c => c != delim).Skip(1);
    StringBuilder full = new StringBuilder(part);
    return full.Append(RemoveQuotes(afterIgnore)).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