6

I'm wondering what's people using to correctly capitalize english strings since ''capitalize'' won't do the job:

(capitalize "can't")
=> "Can'T"

Although writing this function is trivial, I'm wondering if there's a preferred built-in way to do it.

4 Answers 4

8

Maybe if you temporarily add ' to the current word constituent syntax table:

(modify-syntax-entry ?' "w")

(capitalize "can't")
=> "Can't"
1
  • 2
    +1, one caution with this however: (capitalize "this is a 'test'") "This Is A 'test'"
    – cobbal
    Aug 22, 2009 at 5:14
5

I have M-c bound to capitalize-word, and it works to capitalize can't correctly.

4
  • capitalize-word works because it sees "can't" as two words, can and t. This is why originally said strings instead of words. With capitalize-word I'd have to loop over all the words in the string and it'd end up capitilizng the "t"s. Aug 22, 2009 at 16:58
  • 1
    Nope, that's not right. capitalize-word sees can't as a single word. It does not capitalize the t. But it is an interactive function that capitalizes the following word (or N awords) in the current buffer. It is not a function that applies to the passed string. So it wouldn't work for you, anyway.
    – Cheeso
    Aug 22, 2009 at 17:21
  • I thought the same thing but if you run M-2 M-x capitalize-word on "can't" you'll get "Can'T". So, it does see "can't" as two words.
    – seth
    Aug 25, 2009 at 6:49
  • 1
    just did this twice. In a text file, it works as I described, treating can't as a single word. When the word "can't" appears in a comment inside a C-Sharp file, it works as you described, treating can't as 2 words.
    – Cheeso
    Aug 25, 2009 at 15:50
1

Current answers are fine, but if you work with strings in code, you can use s string manipulation library. s-capitalize capitalizes the first word in a sentence.

ELISP> (s-capitalize "can't win the war on drugs in a prison, where the hell you gonna win it?")
"Can't win the war on drugs in a prison, where the hell you gonna win it?"
ELISP> (s-join " " (-map 's-capitalize (s-split " " "can't win the war on drugs in a prison, where the hell you gonna win it?")))
"Can't Win The War On Drugs In A Prison, Where The Hell You Gonna Win It?"

s-titleize capitalizes every word in a string, but it's a simple wrapper around built-in capitalize, therefore Karl Voigtland's workaround applies.

ELISP> (s-titleize "Girl, you can't even think of calling this shit a war.")
"Girl, You Can'T Even Think Of Calling This Shit A War."
ELISP> (progn (modify-syntax-entry ?' "w") (s-titleize "Girl, you can't even think of calling this shit a war."))
"Girl, You Can't Even Think Of Calling This Shit A War."
1

The behavior depends of course on the syntax-table in use, i.e. depends on the major-mode. If the ' character has the "w p" syntax, then it should work correctly. This is the case in text-mode, but not in most programming modes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.