回答
方式一:
str = "Hello World"
str[0] = str[0].downcase
str #=> "hello World"
不'downcase一個新的字符串!'(要認識到它返回'nil'如果第一個字母是小寫)? –
@CarySwoveland很好的觀察。 –
比我的更簡潔。我喜歡這個解決方案。 –
def downcase_first_letter(str)
str[0].downcase + str[1..-1]
end
puts downcase_first_letter('Hello World') #=> hello World
str = "Hello World"
str = str[0,1].downcase + str[1..-1] #hello World
當然,你可以做到這一點更直列過或創建一個方法。
我的另一個答案修改現有的字符串,該技術不:
str = "Hello World"
str2 = str.sub(str[0], str[0].downcase)
str #=> "Hello World"
str2 #=> "hello World"
你的兩個答案是毫米級的,應該合併成一個答案。 –
@CarySwoveland我想使用sub'和分配的'來保證這個新的變量是(哪怕是微小)不同的答案。 –
string = 'Hello World'
puts string[0].downcase #This will print just the first letter in the lower case.
櫃面要打印休息第一個字符的downcase字符串的使用
string = 'Hello World'
newstring = string[0].downcase + string[1..-1]
puts newstring
- 1. 紅寶石 - 用另一個字符串
- 2. 紅寶石 - 從字符串獲得第n個字符
- 3. 紅寶石從字符串中刪除第一個字
- 4. 紅寶石:字符從字符串
- 5. 如何從紅寶石字符串中提取子字符串?
- 6. 紅寶石多個字符串匹配
- 7. 紅寶石 - 字符串內的評估
- 8. 紅寶石to_yaml字符串我的json
- 9. []紅寶石字符串的方法
- 10. 紅寶石的eval字符串函數
- 11. 紅寶石:UNESCAPE unicode字符串
- 12. 轉換紅寶石字符串BIGNUM
- 13. 紅寶石 - 字符串操作
- 14. 紅寶石字符串分割問題
- 15. 紅寶石 - 從字符串中刪除
- 16. 從字符串中提取 - 紅寶石
- 17. 紅寶石to_yaml UTF8字符串
- 18. 字符串替換紅寶石
- 19. 紅寶石字符串連接
- 20. 紅寶石:字符串替換零件
- 21. 紅寶石:字符串比較問題
- 22. 對齊字符串紅寶石
- 23. CONVER零到字符串紅寶石POST
- 24. 紅寶石字符串比較:== VS ===
- 25. 分割字符串與紅寶石
- 26. 紅寶石解析字符串
- 27. 紅寶石字符串連接(我想?)
- 28. 紅寶石推字符串數組
- 29. 獲取字符串數組從純字符串紅寶石
- 30. 字符串包含另一個字符串或正則表達式(紅寶石)
您可能需要使用包括在輸出大寫字符的例子。 – Stefan
@CarySwoveland我想只將第一個字母轉換爲小寫。這是更好地創建具有所需性能 –