我最近开始学习Groovy.我有一个小任务:将字符串中的所有小写单词作为List
我写了下一个代码:
public List<String> findWordsInLowercase(String string){
return string.findAll(/\b[a-z]+\b/)
}
Run Code Online (Sandbox Code Playgroud)
这行得通.但我想在没有正则表达式的情况下这样做,因为它很难阅读,理解和记忆.
现在我尝试编写没有正则表达式的相同功能.我的代码:
public List<String> findWordsInLowercase(String string){
def words = string.split()
words.findAll
{it -> for(Character character in it)
character.isLowerCase()}
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用:(
有类产品:
class Product {
static hasMany = [attributeValues: ProductAttributeValue]
String productId
String manufacturer
BigDecimal price
static constraints = {
productId unique: true, blank: false
manufacturer blank: false
price min: BigDecimal.ZERO
}
}
Run Code Online (Sandbox Code Playgroud)
我想找到所有产品,哪个productId包含子串'filter'.我写了下一个代码:
Product.findAll {it.productId.contains(filter)}
Run Code Online (Sandbox Code Playgroud)
但它不起作用.为什么?