Ruby has a few methods for changing the case of strings.
(Ruby有几种更改字符串大小写的方法。)
To convert to lowercase, use downcase
: (要转换为小写,请使用downcase
:)
"hello James!".downcase #=> "hello james!"
Similarly, upcase
capitalizes every letter and capitalize
capitalizes the first letter of the string but lowercases the rest:
(同样, upcase
字母将每个字母capitalize
, capitalize
字母将第一个字母大写,其余字母小写:)
"hello James!".upcase #=> "HELLO JAMES!"
"hello James!".capitalize #=> "Hello james!"
"hello James!".titleize #=> "Hello James!"
If you want to modify a string in place, you can add an exclamation point to any of those methods:
(如果要在适当位置修改字符串,则可以将惊叹号添加到以下任何方法中:)
string = "hello James!"
string.downcase!
string #=> "hello james!"
Refer to the documentation for String for more information.
(有关更多信息,请参考String文档 。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…