红宝石 |字符串计数()方法
count是 Ruby 中的 String 类方法。在此方法中,每个参数定义了一组要计数的字符。这些集合的交集定义了要在给定字符中计数的字符串。任何其他以插入符号 ^ 开头的字符串都被否定。
Syntax:str.count(parameter_list)
Parameters: Here, str is the given string.
Returns: The numbers of the characters.
示例 1:
# Ruby program to demonstrate
# the count method
# Taking a string and
# using the method
str = "String Counting"
puts str.count "ing"
输出:
7
示例 2:
# Ruby program to demonstrate
# the count method
# Taking a string and
# using the method
str = "String Counting"
puts str.count "^ing"
str2 = "Ruby Method\\r\\n"
puts str.count "\\"
输出:
8
0