groovy - 可以切换使用String.contains吗?

标签 groovy

我正在努力使这项工作,但似乎不被接受。是否有任何修复或解决方法?

switch(email){
    case (email.contains('acceptall')):
        log.info email
        break
    default:
        log.info "Doesn't work!"    
}

我在 SoapUI 中使用 groovy 作为脚本

最佳答案

使用正则表达式,大致:

switch (email) {
  case ~/.*acceptall.*/:
    log.info(email)
    break;
  default:
    log.info("Doesn't work!")
}

http://groovy.codehaus.org/Logical+Branching#LogicalBranching-switchstatement

Switch supports the following kinds of comparisons

  • Class case values matches if the switchValue is an instanceof the class
  • Regular expression case value matches if the string of the switch
  • Value matches the regex Collection case value matches if the switch
  • Value is contained in the collection. This also includes ranges too (since they are Lists)
  • if none of the above are used then the case value matches if the case value equals the switch value

关于groovy - 可以切换使用String.contains吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25666635/

相关文章:

jenkins - 如何限制 Jenkins 并发多分支管道构建?

java - Grails 3.3.9-期待 “interface”,找到 '<newline>'

java - 使用 this 关键字作为 Jenkins 文件中类构造函数的参数

java - 在 grails 服务器内运行 .sh 文件

groovy - 无法插入敏感的环境变量

scala - 从Groovy调用Scala:如何处理不同的集合类型?

android - Android 上的 Groovy CompileStatic 打乱了 Groovy Truth

java - Groovy Mockito NullPointerException

spring - 多个 Camel 路线处理同一个文件

groovy - 从 HTTP 请求获取纯 HTML