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

P

Peter Tosh

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
 
K

Kendall Gifford

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
 
J

Justin Collins

Peter said:
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
 
P

Peter Tosh

Justin said:
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
 
S

Sanjay Sharma

Peter said:
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
 
J

Justin Collins

Peter said:
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
 
S

Sebastian Hungerecker

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.
 
P

Peter Tosh

Justin said:
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! ;)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,809
Latest member
moe77

Latest Threads

Top