Ashish Jambhulkar
Ashish Jambhulkar

Reputation: 1504

Upcase particular letter from string in ruby

Lets say that I have

str = "ashishjambhulkar"

and i want to upcase only "a" after "j" which results into

str = "ashishjAmbhulkar"

How can I do this in ruby?

I have tried something like this

"ashishjambhulkar".split(//).map{ |x| x=="a" ? "A":x }.join('')

but it updates all the a's in the given string.

Upvotes: 1

Views: 101

Answers (1)

Leo
Leo

Reputation: 1773

str.gsub('ja', 'jA')
str.gsub(/(?<=j).{1}/) { |char| char.capitalize } #for any char that coming after 'j'

More about gsub and more about regexp

Upvotes: 6

Related Questions