let source = "Hello world"
let target = ["a","e","i","o","u"]
let firstOrders = target.map { (character) -> Int? in
let index = source.firstIndex(where: {
String($0) == character
})
return index?.utf16Offset(in: source)
}
print(zip(target, firstOrders.compactMap({$0 ?? -1})).map({$0}))
感觉还是不够简明扼要
展开
作者回复: let source = "Hello world"
let target: [Character] = ["a","e","i","o","u"]
zip(0..<source.count, source).forEach { (index, char) in
if target.contains(char) {
print(index)
}
}
因为swift没有yield,所以不是很优雅