红宝石 |字符串大写!方法
大写!是 Ruby 中的 String 类方法,用于通过将给定字符串的第一个字符转换为大写并将其余字符转换为小写来返回给定字符串的副本。大写和大写的唯一区别!方法就是大写!如果没有进行任何更改,方法将返回nil 。
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. And nil if no changes are made.
示例 1:
# Ruby program to demonstrate
# the capitalize! method
# Taking the string and
# using the method
puts "ruby".capitalize!()
puts "gfg".capitalize!()
输出:
Ruby
Gfg
示例 2:
# Ruby program to demonstrate
# the capitalize! method
# Taking a string
# already in uppercase
# it will not give any
# output
puts "Geeksforgeeks".capitalize!()
puts "computer".capitalize!()
输出:
Computer