我似乎无法获得如何大写字符串的第一行
"1. walking down the street
2. to go to the store
3. to buy some groceries"
答案 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