Ruby:如何计算一个字符串在另一个字符串中出现的次数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25938430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:34:50  来源:igfitidea点击:

Ruby: How to count the number of times a string appears in another string?

rubystringcount

提问by Johnson

I'm trying to count the number of times a string appears in another string.

我正在尝试计算一个字符串在另一个字符串中出现的次数。

I know you can count the number of times a letter appears in a string:

我知道你可以计算一个字母出现在字符串中的次数:

string = "aabbccddbb"
string.count('a')
=> 2

But if I search for how many times 'aa' appears in this string, I also get two.

但是如果我搜索 'aa' 在这个字符串中出现的次数,我也会得到两次。

string.count('aa')
=> 2

I don't understand this. I put the value in quotation marks, so I'm searching for the number of times the exact string appears, not just the letters.

我不明白这个。我将值放在引号中,因此我正在搜索确切字符串出现的次数,而不仅仅是字母。

回答by Cary Swoveland

Here are a couple of ways to count the numbers of times a given substring appears in a string (the first being my preference). Note (as confirmed by the OP) the substring 'aa'appears twice in the string 'aaa', and therefore five times in:

这里有几种方法来计算给定子字符串在字符串中出现的次数(第一种是我的偏好)。注意(由 OP 确认)子字符串'aa'在 string 中出现两次'aaa',因此在:

string="aaabbccaaaaddbb"

#1

#1

Use String#scanwith a regex that contains a positive lookahead that looks for the substring:

String#scan与包含查找子字符串的正向前瞻的正则表达式一起使用:

def count_em(string, substring)
  string.scan(/(?=#{substring})/).count
end

count_em(string,"aa")
 #=> 5

Note:

笔记:

"aaabbccaaaaddbb".scan(/(?=aa)/)
  #=> ["", "", "", "", ""]

A positive lookbehind produces the same result:

正向后视产生相同的结果:

"aaabbccaaaaddbb".scan(/(?<=aa)/)
  #=> ["", "", "", "", ""]

As well, String#scancan be replaced with String#gsub.

同样,String#scan可以替换为String#gsub

#2

#2

Convert to an array, apply Enumerable#each_cons, then join and count:

转换为数组,应用Enumerable#each_cons,然后加入并计数:

def count_em(string, substring)
  string.each_char.each_cons(substring.size).map(&:join).count(substring)
end

count_em(string,"aa")
  #=> 5

We have:

我们有:

enum0 = "aaabbccaaaaddbb".each_char
  #=> #<Enumerator: "aaabbccaaaaddbb":each_char>

We can see the elements that will generated by this enumerator by converting it to an array:

我们可以通过将其转换为数组来查看此枚举器将生成​​的元素:

enum0.to_a
  #=> ["a", "a", "a", "b", "b", "c", "c", "a", "a", "a",
  #    "a", "d", "d", "b", "b"]

enum1 = enum0.each_cons("aa".size)
  #=> #<Enumerator: #<Enumerator: "aaabbccaaaaddbb":each_char>:each_cons(2)> 

Convert enum1to an array to see what values the enumerator will pass on to map:

转换enum1为数组以查看枚举器将传递给哪些值map

enum1.to_a
  #=> [["a", "a"], ["a", "a"], ["a", "b"], ["b", "b"], ["b", "c"],
  #    ["c", "c"], ["c", "a"], ["a", "a"], ["a", "a"], ["a", "a"], 
  #    ["a", "d"], ["d", "d"], ["d", "b"], ["b", "b"]]

c = enum1.map(&:join)
  #=> ["aa", "aa", "ab", "bb", "bc", "cc", "ca",
  #    "aa", "aa", "aa", "ad", "dd", "db", "bb"]
c.count("aa")
  #=> 5

回答by tadman

It's because the countcounts characters, not instances of strings. In this case 'aa'means the same thing as 'a', it's considered a set of characters to count.

这是因为count计数字符,而不是字符串的实例。在这种情况下'aa',与 含义相同'a',它被认为是一组要计算的字符。

To count the number of times aaappears in the string:

要计算aa字符串中出现的次数:

string = "aabbccddbb"
string.scan(/aa/).length
# => 1
string.scan(/bb/).length
# => 2
string.scan(/ff/).length
# => 0

回答by Georgii Gavrilchik

try to use string.split('a').count - 1

尝试使用 string.split('a').count - 1