检查字符串是否包含子字符串。此外,获取索引和匹配数 (Raku)

Check if a string contains a substring. Additionally, get index and number of match (Raku)

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

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: 例程 index and rindex 很酷,但只能匹配一次。

相关链接:

  1. 要检查它是否包含,请使用 .contains to get a Bool which is a cool method
  2. 要获取索引(别名索引:两者都是索引的复数)使用.indices
  3. 要获得数字,请计算索引。
"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:例程indices is described just after index and rindex。所以请阅读好文档,并好好阅读它 ;-)

  • 链接:

    • Python, Perl
    • 中的相同问题