I'm trying to write a helper to translate a string from "something_like_this"
to "Something like this"
. I'm using "something_like_this".titlecase
to get it to "Something Like This"
but I'm stuck lowercasing every uppercase letter except the first.
I suppose I'm looking for something like this:
def write_sentence
string.titlecase.gsub!(/UPPERCASE-TO-LOWERCASE-EXCEPT-FIRST/)
#that should be something to lowercase everything except the first letter
return string
end
So in the view I could just write string.write_sentence
and have it return exactly what I want. Any thoughts?
Thanks!
EDIT
I should mention that the string can sometimes be just one word, in which case the string should be converted from "something"
to "Something"
.
Sentence case is when a heading is written like a sentence, with a capital initial on the first word, followed by lowercase initials for the rest of the heading, like this: “Hello world” Title case is when a heading is written with capital initials for all its words, like “Hello World”
"Barack Obama flies to thank troops who killed Bin Laden." "FBI investigating Cardinals' alleged hacking of Astros' computer system." "Only the first word and proper nouns are capitalized..."
What Is Title Case? Title case is a style that is traditionally used for the titles of books, movies, songs, plays, and other works. In title case, all major words are capitalized, while minor words are lowercased. A simple example would be Lord of the Flies.
Try this,
"something_like_this".humanize
http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-humanize
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With