如果我有两个如下所示的字符串列表,并且想基于startsWith
逻辑创建一个新的字符串列表,该怎么办?
pathList = ["/etc/passwd/something.txt",
"/etc/fonts/fonts.conf"
"/var/www/foo.bar",
"/some/foo/path/one.txt"]
notAllowedPathList = ["/etc/fonts",
"/var",
"/path"]
newList = ["/etc/passwd/something.txt", "/some/foo/path/one.txt"]
newList
通过查看中的每个元素中的pathList
startsWith
每个元素来创建newList
。
所以,从以上pathList
,/etc/fonts/fonts.conf
并且/var/www/foo.bar
被删除,因为它们startWith/etc/fonts
和/var
分别。
我在下面提出了建议,但我相信这样做会有更时髦的方法:
def newList = []
pathList.each {String fileName ->
notAllowedPathList.find { String notAllowed ->
if (fileName.startsWith(notAllowed)) {
return true
}
else {
newList << fileName
}
}
}
def pathList = [
"/etc/passwd/something.txt",
"/etc/fonts/fonts.conf",
"/var/www/foo.bar",
"/some/foo/path/one.txt"
]
def notAllowedPathList = ["/etc/fonts", "/var", "/path"]
def newList = pathList.findAll { String fileName ->
!notAllowedPathList.any {
fileName.startsWith(it)
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句