[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Quickest way to do this string op.

eastcoastcoder

3/13/2006 1:20:00 PM

What's the quickest way to turn "BOCA RATON" into "Boca Raton"?

3 Answers

Carlos

3/13/2006 1:31:00 PM

0

eastcoastcoder@gmail.com wrote:

> What's the quickest way to turn "BOCA RATON" into "Boca Raton"?

"BOCA RATON".replace "Boca Raton" # :-P

Probably

"BOCA RATON".split(/\b/).map{|s| s.capitalize }.join

It is quick to write, at least...

HTH




Tassilo Horn

3/13/2006 1:32:00 PM

0

eastcoastcoder@gmail.com writes:

> What's the quickest way to turn "BOCA RATON" into "Boca Raton"?

irb(main):006:0> str = "BOCA RATON"
irb(main):009:0> str.gsub!(/\w+/) { |s| s.capitalize }
=> "Boca Raton"
irb(main):010:0> str
=> "Boca Raton"

Hope this helps. But I'm not sure if that's the fastest way in meaning
of speed. At least it's the solution which crossed my mind first. ;-)

Regards,
Tassilo

pinki

3/13/2006 9:14:00 PM

0

I'm also new, but this might be another solution:

"BOCA RATON".split(' ').collect do |name| name.capitalize end.join(' ')

It's not the quickest way though..