【问题标题】:Groovy/Grails Contains with LowercaseGroovy/Grails 包含小写字母
【发布时间】:2023-04-09 21:38:01
【问题描述】:

我想检查一个列表是否包含特定的字符串。

在检查列表中的所有条目之前,sting 应该是小写的

我试过这样

 def venueName = params.name
 def venueNameLists = Venue.executeQuery("select name from Venue")
  if(venueNameLists.toLowerCase().contains(venueName.toLowerCase())){
            error = true;
            log.debug("save :: duplicate name")
            flash.message = "Venue name already exist";
            render(view: "create", model: [venueInstance: new Venue(params)])
            return
        }

报错

  No signature of method: java.util.ArrayList.toLowerCase() is applicable for argument types: () values: []. Stacktrace follows:

  groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.toLowerCase() is applicable for argument types: () values: []

【问题讨论】:

    标签: java list grails groovy


    【解决方案1】:

    我同意aiolos:使用约束或尝试按名称查找实例忽略大小写。但是要以您的方式解决此问题,请尝试*. (spread operator):

    venueNameLists*.toLowerCase().contains(venueName.toLowerCase()) 
    

    【讨论】:

    • 您好,知道如何解决该错误:CPS 转换尚不支持传播
    【解决方案2】:

    如果您想在保存元素之前检查重复条目,请在您的域类上使用constraints。在这里,您可以使用unique 约束,如果需要,您可以实现自己的case insensitive

    如果你需要手动检查,试试这个:

    def venueWithNameFromParams = Venue.findByNameIlike(params.name) // ignore case
    if(venueWithNameFromParams){
        // venueName is in venueNameList
    } 
    

    【讨论】:

      【解决方案3】:

      如果您正在寻找如何检查多个字符串是否包含一个单词,同时忽略区分大小写,请在单词的正则表达式上使用 (?i)。

      例如,以下将是肯定条件:

      word = "YES"
      word.matches(/(?i)yes|ok|true/)
      

      【讨论】:

        猜你喜欢
        • 2016-08-18
        • 2013-04-28
        • 2011-05-23
        • 1970-01-01
        • 2012-02-29
        • 1970-01-01
        • 1970-01-01
        • 2012-02-06
        相关资源
        最近更新 更多