-
StackOverflow 文档
-
Swift Language 教程
-
字符串和字符
-
从 Set 中未定义的字符串中删除字符
Version = 2.2
func removeCharactersNotInSetFromText(text: String, set: Set<Character>) -> String {
return String(text.characters.filter { set.contains( $0) })
}
let text = "Swift 3.0 Come Out"
var chars = Set([Character]("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ".characters))
let newText = removeCharactersNotInSetFromText(text, set: chars) // "SwiftComeOut"
Version = 3.0
func removeCharactersNotInSetFromText(text: String, set: Set<Character>) -> String {
return String(text.characters.filter { set.contains( $0) })
}
let text = "Swift 3.0 Come Out"
var chars = Set([Character]("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ".characters))
let newText = removeCharactersNotInSetFromText(text: text, set: chars)