我想知道人们在正确地大写英文字符串时使用的方法,因为“capitalize”不能完成这项工作:
虽然编写这个函数很简单,但我想知道是否有首选的内置方法来完成它。
(capitalize "can't")
=> "Can'T"
虽然编写这个函数很简单,但我想知道是否有首选的内置方法来完成它。
'
添加到当前单词成分语法表中:(modify-syntax-entry ?' "w")
(capitalize "can't")
=> "Can't"
我将M-c按键绑定为capitalize-word,它可以正确地将can't
大写。
目前的答案很好,但如果您在代码中使用字符串,可以使用s
字符串操作库。s-capitalize
将句子中的第一个单词大写。
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
函数可以将字符串中的每个单词首字母大写,但它只是对内置的capitalize
方法的简单封装,因此需要使用Karl Voigtland的解决方案。
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."