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

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

On Jun 12, 11:42 am, Peter T. [email protected] 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 = “c:\\test” # creates w/double backslashes
irb(main):002:0> puts a.sub(/\\/, “\”) # prints replaced version

Peter T. 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

Justin C. wrote:

Peter T. 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

Peter T. 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

Am Freitag 12 Juni 2009 20:35:43 schrieb Peter T.:

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.

Peter T. 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

Justin C. wrote:

Peter T. 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! :wink: