[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Swapping characters in a string

seebs

4/30/2007 2:09:00 AM

In message <F5cZh.387091$5j1.283606@bgtnsc04-news.ops.worldnet.att.net>, "Michael W. Ryder" wri
tes:
>Is there any way in Ruby, short of stepping through a string character
>by character, to swap two characters? For example, if I want to replace
>all of the commas with periods and periods with commas.

Shouldn't String#tr do it?

foo.tr!(',.', '.,')

-s

1 Answer

Michael W. Ryder

4/30/2007 2:59:00 AM

0

Peter Seebach wrote:
> In message <F5cZh.387091$5j1.283606@bgtnsc04-news.ops.worldnet.att.net>, "Michael W. Ryder" wri
> tes:
>> Is there any way in Ruby, short of stepping through a string character
>> by character, to swap two characters? For example, if I want to replace
>> all of the commas with periods and periods with commas.
>
> Shouldn't String#tr do it?
>
> foo.tr!(',.', '.,')
>
> -s
>
Yes that works fine. For some reason when I saw that method I didn't
think it could swap characters. That's what I get for not experimenting
first. Thanks for the help.