红宝石 |字符串 chars() 方法
chars是 Ruby 中的 String 类方法,用于返回 str 中的字符数组。
Syntax: str.chars
Parameters: Here, str is the given string
Returns: An array of the characters.
示例 1:
# Ruby program to demonstrate
# the chars method
# Taking a string and
# using the method
puts "Ruby String".chars()
puts "Methods".chars()
输出:
R
u
b
y
S
t
r
i
n
g
M
e
t
h
o
d
s
示例 2:
# Ruby program to demonstrate
# the chars method
# Taking a string and
# using the method
puts "GFG".chars()
puts "G4G".chars()
输出:
G
F
G
G
4
G