从 NSString 中去除非字母数字字符 [英] Strip Non-Alphanumeric Characters from an NSString

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

问题描述

我正在寻找一种快速简便的方法来从 NSString 中去除非字母数字字符.可能是使用 NSCharacterSet 的东西,但我很累,似乎没有任何东西返回只包含字符串中字母数字字符的字符串.

I'm looking for a quick and easy way to strip non-alphanumeric characters from an NSString. Probably something using an NSCharacterSet, but I'm tired and nothing seems to return a string containing only the alphanumeric characters in a string.

推荐答案

我们可以通过拆分然后加入来实现.组件SeparatedByCharactersInSet 需要 OS X 10.5+:

We can do this by splitting and then joining. Requires OS X 10.5+ for the componentsSeparatedByCharactersInSet:

NSCharacterSet *charactersToRemove = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
NSString *strippedReplacement = [[someString componentsSeparatedByCharactersInSet:charactersToRemove] componentsJoinedByString:@""];

这篇关于从 NSString 中去除非字母数字字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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