所有字母小写,除了句子中的第一个单词

时间:2013-11-15 09:54:02

标签: ruby regex

我有一个字符串

s = "Test TEst TEST. Test. TEST. TEST. Test test test."

我需要使结果像这样

s = "Test test test. Test. Test. Test. Test test test."

我如何在ruby上做到这一点?

THX。

2 个答案:

答案 0 :(得分:3)

s =  "Test TEst TEST. Test. TEST. TEST. Test test test."
p s.split('. ').map(&:capitalize).join('. ')
#=> "Test test test. Test. Test. Test. Test test test."

答案 1 :(得分:2)

s.gsub(/\w[^.]*/, &:capitalize)
# => "Test test test. Test. Test. Test. Test test test."