字符串 count() 方法 [英] The string count() method

查看:35
本文介绍了字符串 count() 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

忙着学习Ruby……文档里有个例子:

Busy learning Ruby... the documentation have an example:

"hello world".count("lo", "o") 返回 2 怎么返回 2?

"hello world".count("lo", "o") that return 2 how does that return 2?

在我的例子中,我有: puts "Lennie".count("Le", "ie") 返回 2.

In my example I've: puts "Lennie".count("Le", "ie") that return 2.

在这方面计数如何工作?

How does count work in this regard?

推荐答案

"hello world".count("lo") 返回五个.它匹配了第三、第四、第五、第八和第十个字符.让我们称之为一组.

"hello world".count("lo") returns five. It has matched the third, fourth, fifth, eighth, and tenth characters. Lets call this set one.

"hello world".count("o") 返回两个.它匹配了第五个和第八个字符.让我们称之为第二组.

"hello world".count("o") returns two. It has matched the fifth and eighth characters. Lets call this set two.

"hello world".count("lo", "o") 计算集合一和二的交集.

"hello world".count("lo", "o") counts the intersection of sets one and two.

intersection 是第三个集合,包含集合二的所有元素也是在第一套.在我们的示例中,集合一和二都包含字符串中的第五个和第八个字符.一共两个字.所以,count 返回两个.

The intersection is a third set containing all of the elements of set two that are also in set one. In our example, both sets one and two contain the fifth and eighth characters from the string. That's two characters total. So, count returns two.

这篇关于字符串 count() 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆