红宝石 |字符串字节大小方法
bytesize是 Ruby 中的 String 类方法,用于获取给定字符串的长度(以字节为单位)。
Syntax: str.bytesize
Parameters: Here, str is the given string.
Returns: This method returns the length of the str in bytes.
示例 1:
# Ruby program to demonstrate
# the bytesize method
# Taking a string and
# using the method
puts "GeeksforGeeks".bytesize
puts "Ruby".bytesize
输出:
13
4
示例 2:
# Ruby program to demonstrate
# the bytesize method
# Taking a string and
# using the method
puts "String Class".bytesize
puts "Methods".bytesize
输出:
12
7