【发布时间】:2018-08-30 02:09:19
【问题描述】:
我正在尝试将字符串的第一个字母转换为大写字符。我正在尝试转向:
"this is a test sentence"
进入:
"This Is A Test Sentence"
这是我的代码:
def first_upper(str)
arr_str = str.split
arr_str.map do |item|
item.capitalize
end
arr_str = arr_str.join(' ')
puts arr_str
end
first_upper('this is a test sentence')
# >> this is a test sentence
如果我将 pry 的 binding.pry 放在那里,item.capitalize 在 map 循环内工作。
我没有正确使用map 方法吗?
【问题讨论】:
-
试试
map! -
啊,所以我用地图创建了一个数组,但没有将它分配给任何东西。地图!替换原始数组的元素。谢谢!
-
你说的是:“把字符串的第一个字母变成大写字符”,你所期待的不一样。