红宝石 |字符串大写()方法
capitalize是 Ruby 中的 String 类方法,用于通过将给定字符串的第一个字符转换为大写并将其余字符转换为小写来返回给定字符串的副本。
Syntax: str.capitalize
Parameters: Here, str is the given string which is to be converted.
Returns: Copy of the string with the first character in uppercase and remaining in lowercase.
示例 #1:
# Ruby program to demonstrate
# the capitalize method
# Taking the string and
# using the method
puts "geeks".capitalize()
puts "hey".capitalize()
输出:
Geeks
Hey
示例 #2:
# Ruby program to demonstrate
# the capitalize method
# Taking a string already in
# uppercase and using the
# method will give the same
# string
puts "Computer".capitalize()
puts "science".capitalize()
输出:
Computer
Science