Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Remove double backslash in string "c:\\test" (http://www.velocityreviews.com/forums/t858020-remove-double-backslash-in-string-c-test.html)

Peter Tosh 06-12-2009 05:42 PM

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
--
Posted via http://www.ruby-forum.com/.


Kendall Gifford 06-12-2009 05:53 PM

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


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 06-12-2009 06:05 PM

Re: Remove double backslash in string "c:\\test"
 
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 06-12-2009 06:35 PM

Re: Remove double backslash in string "c:\\test"
 
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/.


Sanjay Sharma 06-12-2009 06:41 PM

Re: Remove double backslash in string "c:\\test"
 
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/.


Justin Collins 06-12-2009 06:45 PM

Re: Remove double backslash in string "c:\\test"
 
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 06-12-2009 06:47 PM

Re: Remove double backslash in string "c:\\test"
 
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
01100011001110100101110001110100011001010111001101 110100 - so does your
database. And you compare them using == usually.


Peter Tosh 06-12-2009 07:19 PM

Re: Remove double backslash in string "c:\\test"
 
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/.



All times are GMT. The time now is 08:59 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.