检查字符串是否包含子字符串.此外,获取匹配的索引和数量(乐) [英] Check if a string contains a substring. Additionally, get index and number of match (Raku)

查看:33
本文介绍了检查字符串是否包含子字符串.此外,获取匹配的索引和数量(乐)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FAQ:在 Raku 中,如何检查 String 是否包含子字符串?在哪里和多少次?我想要 3 个功能,例如:

FAQ: In Raku, how to check if a String contains a substring ? Where and how many times ? I would like 3 functions such as:

xxx-bool("az and az and az again", "az");  # True 
xxx-num("az and az and az again", "az");   # 3
xxx-list("az and az and az again", "az");  # (0 7 14) 

PS:例程indexrindex 很酷,但只能得到一场比赛.

PS: Routines index and rindex are pretty cool but only get one match.

相关链接:

推荐答案

  1. 要检查它是否包含,请使用 .contains 获取 Bool 这是一个 方法.
  2. 要获取索引(别名索引:两者都是索引的复数),请使用.indices
  3. 要获得数字,请计算索引.
  1. To check if it contains, use .contains to get a Bool which is a cool method.
  2. To get indices (alias indexes: both are plural of index) use .indices
  3. To get number, count the indices.

"az and az and az again".contains("az");        # True
"az and az and az again".indices("az").elems;   # 3
"az and az and az again".indices("az");         # (0 7 14)

PS:例程索引indexrindex.所以阅读好文档,好好阅读它 ;-)

PS: Routine indices is described just after index and rindex. So read the good doc, and read it well ;-)

  • 链接:

这篇关于检查字符串是否包含子字符串.此外,获取匹配的索引和数量(乐)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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