[ACCEPTED]-simple regex -- replace underscore with a space-string

Accepted answer
Score: 36
str.gsub!(/_/, ' ')

gsub stands for 'global substitution', and the 4 exclamation means it'll change the string 3 itself rather than just return the substituted 2 string.

You can also do it without regexes 1 using String#tr!:

str.tr!('_', ' ')
Score: 31

On rails you can use the simplier .humanize and ruby's 4 .downcase method but be careful as it also strips 3 any final '_id' string (in most cases this 2 is just what you need, even the capitalized 1 first letter)

 => "text string" 
Score: 10

Whoops, I actually had it working--just 2 forgot to update the variable name :P

I was 1 using this:

@id = params[:id]
@title = @id.gsub("_", " ")
Score: 0

Using split and join in rails

"test_string".split('_').join(' ')


More Related questions