Remove double backslash in string "c:\\test"

Discussion in 'Ruby' started by Peter Tosh, Jun 12, 2009.

  1. Peter Tosh

    Peter Tosh Guest

    Hi,

    after lots of trying and searching, I still cant figure out how to
    remove the second backslash in a string:

    "c:\\test"

    I need it with one backslash for a comparison in my db.

    Can anyone do it?

    thanks!
    Niels
    --
    Posted via http://www.ruby-forum.com/.
    Peter Tosh, Jun 12, 2009
    #1
    1. Advertising

  2. On Jun 12, 11:42=A0am, Peter Tosh <> wrote:
    > Hi,
    >
    > after lots of trying and searching, I still cant figure out how to
    > remove the second backslash in a string:
    >
    > "c:\\test"
    >
    > I need it with one backslash for a comparison in my db.
    >


    Here is my working IRB session example:

    irb(main):001:0> a =3D "c:\\\\test" # creates w/double backslashes
    irb(main):002:0> puts a.sub(/\\\\/, "\\") # prints replaced version
    Kendall Gifford, Jun 12, 2009
    #2
    1. Advertising

  3. Peter Tosh wrote:
    > Hi,
    >
    > after lots of trying and searching, I still cant figure out how to
    > remove the second backslash in a string:
    >
    > "c:\\test"
    >
    > I need it with one backslash for a comparison in my db.
    >
    > Can anyone do it?
    >
    > thanks!
    > Niels
    >

    Are you sure there are two backslashes, or is that just the escaping
    backslash?

    irb(main):001:0> "c:\\test"
    => "c:\\test"
    irb(main):002:0> puts "c:\\test"
    c:\test
    => nil
    irb(main):003:0> "c:\\test".length
    => 7


    -Justin
    Justin Collins, Jun 12, 2009
    #3
  4. Peter Tosh

    Peter Tosh Guest

    Justin Collins wrote:
    > Peter Tosh wrote:
    >>
    >> thanks!
    >> Niels
    >>

    > Are you sure there are two backslashes, or is that just the escaping
    > backslash?
    >
    > irb(main):001:0> "c:\\test"
    > => "c:\\test"
    > irb(main):002:0> puts "c:\\test"
    > c:\test
    > => nil
    > irb(main):003:0> "c:\\test".length
    > => 7
    >
    >
    > -Justin


    Thanks Justin, when I print out the length it comes to 7 => it is just
    the escaping backslash.

    But in my program I need to compare it to a string from the database:
    "c:\test". When Ruby internally stores it as "c:\\test", how do I
    compare the two?

    When use the debugger it looks like this:
    (rdb:1) pp @path
    "c:\\Test"
    (rdb:1) pp @path.length
    7


    Niels




    --
    Posted via http://www.ruby-forum.com/.
    Peter Tosh, Jun 12, 2009
    #4
  5. Peter Tosh wrote:
    > Hi,
    >
    > after lots of trying and searching, I still cant figure out how to
    > remove the second backslash in a string:
    >
    > "c:\\test"
    >
    > I need it with one backslash for a comparison in my db.
    >
    > Can anyone do it?
    >
    > thanks!
    > Niels


    Is that a string literal from your source code? Is yes, then you can do
    away with the 'escaping the backslash' fiasco by using single quoted
    strings.

    str = 'c:\test'

    which is equivalent to:

    str = "c:\\test"

    -sos
    --
    Posted via http://www.ruby-forum.com/.
    Sanjay Sharma, Jun 12, 2009
    #5
  6. Peter Tosh wrote:
    > Justin Collins wrote:
    >
    >> Peter Tosh wrote:
    >>
    >>> thanks!
    >>> Niels
    >>>
    >>>

    >> Are you sure there are two backslashes, or is that just the escaping
    >> backslash?
    >>
    >> irb(main):001:0> "c:\\test"
    >> => "c:\\test"
    >> irb(main):002:0> puts "c:\\test"
    >> c:\test
    >> => nil
    >> irb(main):003:0> "c:\\test".length
    >> => 7
    >>
    >>
    >> -Justin
    >>

    >
    > Thanks Justin, when I print out the length it comes to 7 => it is just
    > the escaping backslash.
    >
    > But in my program I need to compare it to a string from the database:
    > "c:\test". When Ruby internally stores it as "c:\\test", how do I
    > compare the two?
    >
    > When use the debugger it looks like this:
    > (rdb:1) pp @path
    > "c:\\Test"
    > (rdb:1) pp @path.length
    > 7
    >
    >
    > Niels
    >


    It is not stored like that internally, that is just how it is displayed.
    You do not need to do anything special to compare them. Is the above the
    string from the database? I would try seeing exactly what you are
    getting from the database, then output them both via the same method
    (pp, p, or puts, just be consistent) and see if they are the same. Then
    try comparing them.

    -Justin
    Justin Collins, Jun 12, 2009
    #6
  7. Am Freitag 12 Juni 2009 20:35:43 schrieb Peter Tosh:
    > But in my program I need to compare it to a string from the database:
    > "c:\test". When Ruby internally stores it as "c:\\test", how do I
    > compare the two?


    ruby internally stores it as
    01100011001110100101110001110100011001010111001101110100 - so does your
    database. And you compare them using == usually.
    Sebastian Hungerecker, Jun 12, 2009
    #7
  8. Peter Tosh

    Peter Tosh Guest

    Justin Collins wrote:
    > Peter Tosh wrote:
    >>>
    >>>

    >> "c:\\Test"
    >> (rdb:1) pp @path.length
    >> 7
    >>
    >>
    >> Niels
    >>

    >
    > It is not stored like that internally, that is just how it is displayed.
    > You do not need to do anything special to compare them. Is the above the
    > string from the database? I would try seeing exactly what you are
    > getting from the database, then output them both via the same method
    > (pp, p, or puts, just be consistent) and see if they are the same. Then
    > try comparing them.
    >
    > -Justin


    Did that, and it turns out the output is exactly the same. Some more
    testing and I found a problem with my query! Works find now. Thanks a
    lot, I learned something! ;)
    --
    Posted via http://www.ruby-forum.com/.
    Peter Tosh, Jun 12, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,429
    Victor Bazarov
    Feb 17, 2005
  2. Harry George
    Replies:
    5
    Views:
    717
    Gerrit Holl
    Jan 19, 2004
  3. Vincent Texier
    Replies:
    3
    Views:
    2,234
    Vincent Texier
    Jul 1, 2004
  4. Fred
    Replies:
    2
    Views:
    355
    John Machin
    Mar 9, 2005
  5. Stef Mientki

    Double backslash in filepaths ?

    Stef Mientki, Apr 14, 2007, in forum: Python
    Replies:
    3
    Views:
    738
    Stef Mientki
    Apr 14, 2007
Loading...

Share This Page