Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capitalize first char and leave others as is

I only want to capitalize the first char and leave the others as is.

For example:

"fooBar".titleize returns "Foo Bar". Should return FooBar.

"foo_Bar".capitalize returns "Foo_bar" Should return Foo_Bar.

Any way I can do this?

like image 972
Christian Fazzini Avatar asked Oct 24 '25 13:10

Christian Fazzini


1 Answers

irb(main):001:0> s = "foo_Bar"
=> "foo_Bar"
irb(main):002:0> s[0] = s[0].upcase
=> "F"
irb(main):003:0> s
=> "Foo_Bar"

Or with regex for in-place substitution:

irb(main):001:0> s = "foo_Bar"
=> "foo_Bar"
irb(main):002:0> s.sub!(/^\w/) {|x| x.upcase}
=> "Foo_Bar"
like image 54
Mchl Avatar answered Oct 27 '25 06:10

Mchl