我正在嘗試根據條件從陣列中洗掉一個專案。如果字串包含特定的子字串,則應從陣列中洗掉所有這些專案。從下面的陣列中,我需要洗掉所有具有字串“-oh-”的專案
下面是我的代碼
myArray = [
"Item1",
"Item2",
"Item3",
"test-oh-test",
"Item4",
"demo-oh-test",
"Item5",
"val-oh-trial"
]
if(myArray.contains('-oh-')
{
myArray.remove(/*Need the syntax here*/)
}
uj5u.com熱心網友回復:
這將洗掉所有匹配的元素:
myArray = [
"Item1",
"Item2",
"Item3",
"test-oh-test",
"Item4",
"demo-oh-test",
"Item5",
"val-oh-trial"
]
myArray.removeAll {it -> it.contains("-oh-")}
assert myArray == ["Item1", "Item2", "Item3", "Item4", "Item5"]
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/440125.html
