在Ruby中,可以使用downcase
和upcase
方法将字符串转换为全小写或全大写。例如:
str = "Hello, World!"
# 转换为全小写
lowercase_str = str.downcase
puts lowercase_str # 输出 "hello, world!"
# 转换为全大写
uppercase_str = str.upcase
puts uppercase_str # 输出 "HELLO, WORLD!"
如果你想要逐个字符地转换大小写,可以使用each_char
方法遍历字符串中的每个字符,然后使用downcase
或upcase
方法进行转换。例如:
str = "HeLlO, WoRlD!"
# 逐个字符转换大小写
converted_str = str.each_char.map { |char| char.downcase }.join
puts converted_str # 输出 "hello, world!"
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:PHP字符串处理怎样实现大小写转换