string - 如何计算 Groovy 中字符串中 charSequence 的出现次数

标签 string groovy

例如,如果这是我的方程字符串,

IF(AND(x>0,x<100),5,IF(AND(x>101,x<200),6,10))

我想计算等式中“IF(”字符串出现的次数。

最佳答案

例如这样:

def s = "IF(AND(x>0,x<100),5,IF(AND(x>101,x<200),6,10))"
assert 2 == s.count("IF(")

在更高级的示例中,您可能需要使用正则表达式。

关于string - 如何计算 Groovy 中字符串中 charSequence 的出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33824053/

相关文章:

grails - Grails 域类必须与数据库绑定(bind)吗?

Grails 4 "Static Type Checking"错误仅适用于日期对象

android - 比较字符串时间和当前时间android

javascript - 使用窗口从字符串执行函数不起作用

c - 如何从字符串中复制 2 个 int?

c# - .NET 从文本框填充 StreamReader

当我创建内部 DSL 时,Groovy DSL : How I can overload any() in Groovy,

java - 使用正则表达式同时强制执行序列和分组?

rest - 在 groovy 中检查 HTTP 状态代码

excel - 使用excel公式在单元格中查找确切的子字符串