CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2004
    Posts
    114

    Escape character problem

    I have file urls stored in a database field. Urls are like this:

    "\\Images\Sunset.jpg"

    Where I retrieve it and store in tha string variable it becomes
    "\\\\Images\\Sunset.jpg";

    @ character can be used with strings like strval=@"\\Images";
    But how do I use it with string variables or Database values? Other than using Replace("\\","\"), can anyone suggest a better solution?

  2. #2
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: Escape character problem

    I don't think by just reading into string the escape chars would get added, I suspect that it is stored in DB that way.
    Regards,
    Ramkrishna Pawar

  3. #3
    Join Date
    May 2004
    Location
    Osijek
    Posts
    61

    Re: Escape character problem

    Quote Originally Posted by shuvo View Post
    Where I retrieve it and store in tha string variable it becomes
    "\\\\Images\\Sunset.jpg";
    How are you checking for this.
    If you just look at variable with debugger, it always escapes it (even if you originally used @ char). If you are watching this through debugger, content is ok.
    Put variable into TextBox or something similar, and you will see it without escaping.

  4. #4
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Escape character problem

    just because the string's value is
    Code:
    "\\\\Images\\Whatever"
    When you're debugging, the printed value when you output it to the console or a textbox for example will still be
    Code:
    \\Images\Whatever
    EDIT: D'oh, beaten to the punch!
    It's not a bug, it's a feature!

  5. #5
    Join Date
    Mar 2004
    Posts
    114

    Re: Escape character problem

    Thanks to all of you for your input.I found out that url is still working with "\\\\", that is returned from the database. I just had to replace the last "\\" with a "/".Seriously .net should improves its handling of escape characters.

  6. #6
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Escape character problem

    Having to replace a backslash with a forward slash has nothing to do with .NET escape characters though ...
    It's not a bug, it's a feature!

  7. #7
    Join Date
    Mar 2004
    Posts
    114

    Re: Escape character problem

    @Foamy...Try replacing "\\" with "\".

  8. #8
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Escape character problem

    I'm not sure what you mean... I single backslash will appear as "\\" in C# since it doubles as an escape character, so it needs to be escaped. Two backslashes will therefore appear as "\\\\".
    This can be avoided by appending a @ to the front of the string like this:
    Code:
    string path = @"C:\Program Files\...";
    It's not a bug, it's a feature!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured