Ruby:如何仅在字符串中大写第一行

时间:2014-02-02 18:52:04

标签: ruby

我似乎无法获得如何大写字符串的第一行

"1. walking down the street
2. to go to the store
3. to buy some groceries"

2 个答案:

答案 0 :(得分:0)

String#sub仅替换第一场比赛。使用此功能:

>> s = "1. walking down the street
2. to go to the store
3. to buy some groceries"

>> puts s.sub(/.*/, &:upcase)
1. WALKING DOWN THE STREET
2. to go to the store
3. to buy some groceries

>> puts s.sub(/.*/) { |line| line.sub(/[a-z]/i, &:upcase) }
1. Walking down the street
2. to go to the store
3. to buy some groceries

根据评论

更新

>> puts s.gsub(/.*/) { |line| line.sub(/[a-z]/i, &:upcase) }
1. Walking down the street
2. To go to the store
3. To buy some groceries

答案 1 :(得分:0)

<强> INPUT:

1. walking down the street
2. to go to the store
3. to buy some groceries

使用此功能:

.sub(/[a-zA-Z]/){|c| c.upcase}

<强>输出:

1. Walking down the street
2. to go to the store
3. to buy some groceries