Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From title case to sentence case

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".

like image 336
t56k Avatar asked Mar 24 '13 23:03

t56k


People also ask

Is title case the same as sentence case?

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”

What is an example of sentence case?

"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 an example of title case?

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.


1 Answers

Try this,

"something_like_this".humanize

http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-humanize

like image 152
Sam Avatar answered Oct 06 '22 22:10

Sam