可以切换使用String.contains? [英] Can switch use String.contains?

查看:390
本文介绍了可以切换使用String.contains?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做这项工作,似乎没有被接受。是否有任何修复或变通方法?

  switch(email){
case(email.contains(' acceptall')):
log.info电子邮件
break
默认值:
log.info不起作用!
}

我在SoapUI中使用groovy脚本

解决方案

使用正则表达式,大致如下:

  switch (email){
case〜/.* acceptall。* /:
log.info(email)
break;
default:
log.info(does not work!)
}

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


交换器支持以下几种比较




  • 如果switchValue是
    类的实例,则类案例值匹配

  • 如果
    开关的字符串

  • 如果
    开关

  • 值包含在集合中,则值匹配正则表达式集合大小写值匹配项。这也包括范围
    (因为它们是列表)

  • 如果以上都不使用,则case
    值匹配,如果case值等于转换值



I'm trying to make this work, and it seems not to be accepted. Is there any fix, or workaround to it?

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

I'm using groovy for a script in SoapUI

解决方案

Use a regex, roughly:

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

这篇关于可以切换使用String.contains?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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