红宝石 | String each_grapheme_cluster 方法
each_grapheme_cluster是 Ruby 中的 String 类方法,用于将给定字符串中的每个字素簇传递给给定块,如果没有给出块,则返回枚举器。
Syntax: str.each_grapheme_cluster
Parameters: Here, str is the given string.
Returns: An enumerator.
示例 1:
# Ruby program to demonstrate
# the each_grapheme_cluster method
# Taking a string and
# using the method
puts "Ruby".each_grapheme_cluster
puts "String".each_grapheme_cluster.to_a.size
输出:
#
6
示例 2:
# Ruby program to demonstrate
# the each_grapheme_cluster method
# Taking a string and
# using the method
puts "Sample".each_grapheme_cluster
puts "Input".each_grapheme_cluster.to_a.size
输出:
#
5