none
Remove *only* double quotes from string? RRS feed

  • Question

  • Hi, 

    • Using C# .net 3.5 

    Have some data that contains two double quotes (side by side) which I wish to remove, however I wish to preserve single occurrences of double quote. 

    e.g. 

    ' "hello", "world" ' is ok 

    ' "hello", "how ""are"" you" ' is not, the "" should be removed.

    Tried lots of different amounts of quotes in the .Replace, they either don;t work or remove all. 

    Console.WriteLine (txt.Replace(@""""""""  ,""));
    
    Console.WriteLine (txt.Replace(@""""  ,""));
    
    

     

    Tuesday, March 17, 2015 11:04 AM

Answers

All replies

  • You should do it like this:

                string a = @" ""hello"", ""how """"are"" you"" ' is not, the """;
                a = a.Replace(@"""""", String.Empty);

    The result is:

     "hello", "how are" you" ' is not, the "

    when you use @ sign before a string variable,

    string   display 

    ""      =   " 

    """"   =   ""

    Tuesday, March 17, 2015 11:11 AM
  • Hi, 

    Thanks for the info. 

    What I want to end up with is 

    ' "hello", "how are you" '

    So just the "" removed, twice in this example. 

    ???   =   """"


    Tuesday, March 17, 2015 11:13 AM
  • just use the following and you will see it is working in removing double quotations:

    Console.WriteLine (txt.Replace(@"""""", String.Empty));

    Tuesday, March 17, 2015 11:19 AM
  • That removes single quotes too. 

    string junk = " \"\"double\"\", \"single\" ";
    Console.WriteLine (junk);
    Console.WriteLine (junk.Replace(@"""""" ,string.Empty));
    returns 

    ""double"", "single"
    double, "single" 

    Tuesday, March 17, 2015 11:27 AM
  • Instead of this 

    string junk = " \"\"double\"\", \"single\" ";

    try with that

    string junk = @" """"double"""", ""single"" ";

    Tuesday, March 17, 2015 11:30 AM
  • Same thing.  I am actually testing it in LINQPad C# scratchpad, might pop it into a little console app to see if that makes any difference.  This is what I'm seeing:

    Tuesday, March 17, 2015 11:35 AM
  • Your LINQPad has an issue than. 

    This is the result from my Visual Studio:

    Tuesday, March 17, 2015 11:40 AM
  • Thanks.  I use LINQPAD all the time for testing out small blacks of code and never have any issues, that said I've never tried double-double quotes.  Think I will let the author now as it's' a superb product otherwise. 

    Cheers

    Tuesday, March 17, 2015 11:44 AM