我发现了这个很棒的扩展,它删除了引号中字符以外的所有内容。
extension String.UnicodeScalarView {
var removeCharacters: String {
return String(filter(("cfh".unicodeScalars).contains))
}
}
print("abcd123efg".unicodeScalars.removeCharacters) // it prints “cf”, my desirable result is to make it print “abd123eg”
它打印“cf”,我想要的结果是让它打印“abd123eg”。 你能帮我反转它以仅删除引号之间的字符并保留其他所有字符吗?
注意:识别 (unicodeScalars) 也很重要,所以请不要删除这部分。
最佳答案
您需要否定对 contains
的调用:
return String(filter { !"cfh".unicodeScalars.contains($0) })
关于swift - 使用扩展名从字符串中删除特定字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54359402/