Substitution problem

Hi,

I’d like to capitalize every word in a string.

I was hoping this expression would do it:

newstr = str.gsub(/\b(\s)?([a-z])/,‘\1\2’.upcase)

but it turns out that as \2 is not evaluated right away,
the .upcase has
no effect. Suggestions?

how 'bout the block form?

newstr = str.gsub(/\b(\s)?([a-z])/) { |s| s.upcase }

that works for me.

Regards,
Barry

Hi,

I’d like to capitalize every word in a string.

I was hoping this expression would do it:

newstr = str.gsub(/\b(\s)?([a-z])/,‘\1\2’.upcase)

but it turns out that as \2 is not evaluated right away,
the .upcase has
no effect. Suggestions?

how 'bout the block form?

newstr = str.gsub(/\b(\s)?([a-z])/) { |s| s.upcase }

that works for me.

Or

newstr = str.gsub(/\w+/) { |s| s.capitalize }

Regards,
Barry

Gavin

···

From: “SHULTZ,BARRY (HP-Israel,ex1)” barry.shultz@hp.com

Thanks guys,

Barry, I like yours the best as it’s closet to mine and because you’re
not using capitalize. Capitalize actually changes all other letters in a
word to be lowercase, which isn’t always desired.

Thanks very much all.

db

···

On Sun, Nov 03, 2002 at 06:04:00PM +0900, SHULTZ,BARRY (HP-Israel,ex1) wrote:

Hi,

I’d like to capitalize every word in a string.

I was hoping this expression would do it:

newstr = str.gsub(/\b(\s)?([a-z])/,‘\1\2’.upcase)

but it turns out that as \2 is not evaluated right away,
the .upcase has
no effect. Suggestions?

how 'bout the block form?

newstr = str.gsub(/\b(\s)?([a-z])/) { |s| s.upcase }

that works for me.

Regards,
Barry


A.D. 1844: Samuel Morse invents Morse code. Cryptography export
restrictions prevent the telegraph’s use outside the U.S. and Canada.