从NSString中删除NSCharacterSet中的字符 [英] Remove characters in NSCharacterSet from NSString
问题描述
我有一个NSCharacterSet,其中包含我要从我的NSString中删除的所有字符.
I have an NSCharacterSet which contains all the characters I want to remove from my NSString.
我该怎么办?
推荐答案
如果您不太担心效率,则简单的方法是[[myString componentsSeparatedByCharactersInSet:myCharacterSet] componentsJoinedByString:@""]
.
If you're not too worried about efficiency, a simple way would be [[myString componentsSeparatedByCharactersInSet:myCharacterSet] componentsJoinedByString:@""]
.
否则,您可以循环遍历这些字符,将不在集合中的字符附加到新字符串上.如果这样做,请记住在构建结果时使用NSMutableString
作为结果.
Otherwise, you could run through the characters in a loop, appending ones that weren't in the set onto a new string. If you do it that way, remember to use an NSMutableString
for your result as you're building it up.
这篇关于从NSString中删除NSCharacterSet中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!