[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Peter Tosh

6/12/2009 5:42:00 PM

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

7 Answers

Kendall Gifford

6/12/2009 5:53:00 PM

0



On Jun 12, 11:42=A0am, Peter Tosh <nl...@yahoo.com> 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

Justin Collins

6/12/2009 6:06:00 PM

0

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

Peter Tosh

6/12/2009 6:36:00 PM

0

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

Sanjay Sharma

6/12/2009 6:42:00 PM

0

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

Justin Collins

6/12/2009 6:45:00 PM

0

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

Sebastian Hungerecker

6/12/2009 6:47:00 PM

0

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.

Peter Tosh

6/12/2009 7:19:00 PM

0

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