none
Efficient way to remove double quotes from a string. RRS feed

  • Question

  • For some reason, I am reading a string out of a config file, and rather than just getting the string, I am getting it in double quotes.

    Here's a clip of my App.Config file

     

    <setting name="TextLogFileName" serializeAs="String">

    <value>C:\CodeTracingFile.txt</value>

    </setting>

     

    When I read this value into a variable, the variable ends up with ""C:\CodeTracingFile.txt""

    I don't know why the double quotes are being added, but I'd like a simple efficient way to get rid of them.

    I tried using String.Replace, but it wants a character, and I don't know how to specify a quote as the item I want replaced.

    Wednesday, August 30, 2006 12:02 AM

Answers

  • ok.

    VB.NET handles the quotes differently - if its actual quotes then it will wrap around 2 quotes to make sure it doesn't end the statement/string.

     

    to remove the quotes, i guess you can read it into a string then remove them:

    dim theString as string = yourString.Remove("""", String.Empty)

     

    Wednesday, August 30, 2006 2:22 PM
    Moderator
  • The above did not work because the Replace does not replace in the current string, but creates a new string. So the code above would have needed to be:

    _filenameandpath=_filenameandpath.Replace("""",string.Empty)

     

    This is also why it worked when you did this:

    ?_filenameandpath.Replace("""",string.Empty)

    Wednesday, August 30, 2006 3:10 PM

All replies

  • Are the double quotes appearing when you view the string in the debugger or when output the string with methods like Debug.WriteLine()?
    Wednesday, August 30, 2006 12:56 AM
    Moderator
  • A quote is ASCII character 0x22.  Does that help?

    Wednesday, August 30, 2006 1:49 AM
  •  duck thing wrote:

    A quote is ASCII character 0x22.  Does that help?

    Strings in .NET are Unicode, not Ascii.
    Wednesday, August 30, 2006 2:11 AM
    Moderator
  •  Peter Ritchie wrote:

    Strings in .NET are Unicode, not Ascii.

    *slaps forehead*  Duh.  Well, looks like it's time for me to get to bed! 

    Wednesday, August 30, 2006 2:16 AM
  • what language are you using? sounds like VB.NET?
    Wednesday, August 30, 2006 2:37 AM
    Moderator
  • When I output the string with debug.print
    Wednesday, August 30, 2006 2:05 PM
  • Yes, VB.NET
    Wednesday, August 30, 2006 2:06 PM
  • ok.

    VB.NET handles the quotes differently - if its actual quotes then it will wrap around 2 quotes to make sure it doesn't end the statement/string.

     

    to remove the quotes, i guess you can read it into a string then remove them:

    dim theString as string = yourString.Remove("""", String.Empty)

     

    Wednesday, August 30, 2006 2:22 PM
    Moderator
  •  ahmedilyas wrote:

    ok.

    VB.NET handles the quotes differently - if its actual quotes then it will wrap around 2 quotes to make sure it doesn't end the statement/string.

     to remove the quotes, i guess you can read it into a string then remove them:

    dim theString as string = yourString.Remove("""", String.Empty)

     

     

    Here's what my immediate window looks like...  still not working.   Any other ideas?

    _filenameandpath.Replace("""",string.Empty)

    ? _filenameandpath

    ""C:\CodeTracingFile.txt""

    Wednesday, August 30, 2006 2:27 PM
  • Ok, for some reason, this seems to have worked...

     

    ? _FileNameAndPath.Replace(char.Parse(""""),string.empty)

    "C:\CodeTracingFile.txt"

    Wednesday, August 30, 2006 2:40 PM
  • I think the immediate/command window will still display it like that, any strings actually I believe it will display it in quotes. Try doing a MessageBox and see what it shows up with.
    Wednesday, August 30, 2006 2:40 PM
    Moderator
  • The above did not work because the Replace does not replace in the current string, but creates a new string. So the code above would have needed to be:

    _filenameandpath=_filenameandpath.Replace("""",string.Empty)

     

    This is also why it worked when you did this:

    ?_filenameandpath.Replace("""",string.Empty)

    Wednesday, August 30, 2006 3:10 PM
  • Add this line at the top:

    using System.Text.RegularExpressions;

     

    and pass your string to this method;

     

    String RemoveQ(string YourString)

    {

    return Regex.Replace(strIn, @"[^\w\.@-]", "");

    }

    Monday, October 8, 2007 7:10 PM