[英]Filter an array by NOT containing string (swift 2)
I am relatively new to swift; 我对迅速相对较新; I am working on filtering arrays. 我正在研究过滤数组。 I know how to filter out elements of an array that contain a letter (like so: let filteredList = wordlist.filter { !$0.characters.contains(letter) }
), but how do I filter out elements that do not have a letter? 我知道如何过滤掉包含字母的数组元素(如: let filteredList = wordlist.filter { !$0.characters.contains(letter) }
),但如何过滤掉没有字母的元素? Here's what I want to accomplish: I have a word list in string-array format, ie ["thing", "other thing"] (but much longer), and I want to return every element that has a certain letter, filtering out the ones that do not have a certain letter. 这就是我想要完成的事情:我有一个字符串数组格式的单词列表,即[“thing”,“other thing”](但更长),我想返回每个具有特定字母的元素,过滤掉那些没有特定字母的。
Thanks in advance. 提前致谢。
This was a silly question, I am sorry. 这是一个愚蠢的问题,对不起。 Anyway, I just needed to remove the exclamation mark. 无论如何,我只需要删除感叹号。 So... 所以...
let filteredList = wordlist.filter { !$0.characters.contains(letter) }
// returns elements in the array WITHOUT "letter".
let filteredList = wordlist.filter { $0.characters.contains(letter) }
// returns elements in the array WITH "letter".
Thanks Eendje. 谢谢Eendje。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.