[ACCEPTED]-simple regex -- replace underscore with a space-string
Accepted answer
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!('_', ' ')
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_id'.humanize.downcase
=> "text string"
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("_", " ")
Using split and join in rails
"test_string".split('_').join(' ')
0
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.