[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

string.tr and \

Thomas Counsell

3/21/2005 10:43:00 AM

Hello

I'm a little confused and would appreciate your help. I would like to
remove any \ characters from a string as well as some other characters.

'b\\c['.tr('\\','') # => 'bc', which is great.

'b\\c'.tr('\\c','.') # => 'b\\' which is not great.

Why does adding extra characters to the tr function stop the removal of
\ characters?

(ruby 1.8.2 (2004-12-25) [powerpc-darwin7.7.0])

Thanks

Tom



6 Answers

Bertram Scharpf

3/21/2005 11:26:00 AM

0

Hi,

Am Montag, 21. Mär 2005, 19:43:18 +0900 schrieb Tom Counsell:
> I'm a little confused and would appreciate your help. I would like to
> remove any \ characters from a string as well as some other characters.
>
> 'b\\c['.tr('\\','') # => 'bc', which is great.
>
> 'b\\c'.tr('\\c','.') # => 'b\\' which is not great.
>
> Why does adding extra characters to the tr function stop the removal of
> \ characters?

The backslash needs to be escaped:

'b\\c'.tr '\\\\c', '.'

As far as I know there's no way to avoid escaping an \ in
string literals.

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...


Carlos

3/21/2005 11:29:00 AM

0

[Tom Counsell <tamc2@cam.ac.uk>, 2005-03-21 11.43 CET]
> Hello
>
> I'm a little confused and would appreciate your help. I would like to
> remove any \ characters from a string as well as some other characters.
>
> 'b\\c['.tr('\\','') # => 'bc', which is great.
>
> 'b\\c'.tr('\\c','.') # => 'b\\' which is not great.
>
> Why does adding extra characters to the tr function stop the removal of
> \ characters?

There are two levels of escaping involved here:

First, in the literal string, you use a \ to escape the \. The method
receives the string \c.

But the method also uses \ to escape (the "^" and the "-":
"555-7819".tr("1\\-9", "") => "55578").

So you must escape the \ with another \. In your string literal you must
write four \'s.

'b\\c'.tr('\\\\c','.') => "b.."

Hope this helps.



Robert Klemme

3/21/2005 12:01:00 PM

0


"Tom Counsell" <tamc2@cam.ac.uk> schrieb im Newsbeitrag
news:193f68dcbb7799e1110cab8eb7a02248@cam.ac.uk...
> Hello
>
> I'm a little confused and would appreciate your help. I would like to
> remove any \ characters from a string as well as some other characters.
>
> 'b\\c['.tr('\\','') # => 'bc', which is great.
>
> 'b\\c'.tr('\\c','.') # => 'b\\' which is not great.
>
> Why does adding extra characters to the tr function stop the removal of
> \ characters?

Note also

>> 'b\\c'.tr('c\\','.')
=> "b.."

You're abusing String#tr - this method is intended to replace single chars
by other single chars. That you can remove characters is a special case
with empty replacement strings. You really should be using gsub or gsub!:

>> 'b\\c'.gsub /\\/, ''
=> "bc"

Kind regards

robert

Christian Neukirchen

3/21/2005 3:08:00 PM

0

"Robert Klemme" <bob.news@gmx.net> writes:

> "Tom Counsell" <tamc2@cam.ac.uk> schrieb im Newsbeitrag
> news:193f68dcbb7799e1110cab8eb7a02248@cam.ac.uk...
>> Hello
>>
>> I'm a little confused and would appreciate your help. I would like to
>> remove any \ characters from a string as well as some other characters.
>>
>
> You're abusing String#tr - this method is intended to replace single chars
> by other single chars. That you can remove characters is a special case
> with empty replacement strings. You really should be using gsub or gsub!:
>
>>> 'b\\c'.gsub /\\/, ''
> => "bc"

String#delete exists.

> robert
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...


Robert Klemme

3/21/2005 3:16:00 PM

0


"Christian Neukirchen" <chneukirchen@gmail.com> schrieb im Newsbeitrag
news:m2mzsxvxyl.fsf@lilith.local...
> "Robert Klemme" <bob.news@gmx.net> writes:
>
> > "Tom Counsell" <tamc2@cam.ac.uk> schrieb im Newsbeitrag
> > news:193f68dcbb7799e1110cab8eb7a02248@cam.ac.uk...
> >> Hello
> >>
> >> I'm a little confused and would appreciate your help. I would like
to
> >> remove any \ characters from a string as well as some other
characters.
> >>
> >
> > You're abusing String#tr - this method is intended to replace single
chars
> > by other single chars. That you can remove characters is a special
case
> > with empty replacement strings. You really should be using gsub or
gsub!:
> >
> >>> 'b\\c'.gsub /\\/, ''
> > => "bc"
>
> String#delete exists.

Cool! Wasn't aware of that. Thx for the pointer!

robert

Bertram Scharpf

3/21/2005 9:16:00 PM

0

Hi,

Am Dienstag, 22. Mär 2005, 00:19:59 +0900 schrieb Robert Klemme:
>
> "Christian Neukirchen" <chneukirchen@gmail.com> schrieb im Newsbeitrag
> news:m2mzsxvxyl.fsf@lilith.local...
> > "Robert Klemme" <bob.news@gmx.net> writes:
> >
> > > "Tom Counsell" <tamc2@cam.ac.uk> schrieb im Newsbeitrag
> > > news:193f68dcbb7799e1110cab8eb7a02248@cam.ac.uk...
> > >> Hello
> > >>
> > >> I'm a little confused and would appreciate your help.
> > >> I would like to remove any \ characters from a string
> > >> as well as some other characters.
> > >>
> > >
> > > You're abusing String#tr - this method is intended to
> > > replace single chars by other single chars. That you
> > > can remove characters is a special case with empty
> > > replacement strings. You really should be using gsub
> > > or gsub!:
> > >
> > >>> 'b\\c'.gsub /\\/, ''
> > > => "bc"
> >
> > String#delete exists.
>
> Cool! Wasn't aware of that. Thx for the pointer!

String#delete will be called when tr's second parameter is
empty.

Bertram


String#delete will be called when tr's second parameter is
empty.

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...