从NSString中删除NSCharacterSet中的字符 [英] Remove characters in NSCharacterSet from NSString

查看:83
本文介绍了从NSString中删除NSCharacterSet中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆