从 NSString 中去除非字母数字字符

我正在寻找一个快速和简单的方法来剥离非字母数字字符从 NSString。可能是使用 NSCharacterSet的东西,但是我累了,似乎没有返回一个只包含字符串中的字母数字字符的字符串。

35075 次浏览

我最后所做的就是创建一个 NSPersonterSet 和我发现的 -invertedSet方法(多睡一个小时对文档阅读能力有什么好处真是个奇迹)。下面是代码片段,假设 someString是要从中删除非字母数字字符的字符串:

NSCharacterSet *charactersToRemove =
[[ NSCharacterSet alphanumericCharacterSet ] invertedSet ];


NSString *trimmedReplacement =
[ someString stringByTrimmingCharactersInSet:charactersToRemove ];

然后,trimmedReplacement将包含 someString的字母数字字符。

我们可以通过分割然后加入操作系统来实现这一点:

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

清理类别

我有一个方法调用 stringByStrippingCharactersInSet:stringByCollapsingWhitespace,可能方便只是下载。

@implementation NSString (Cleanup)


- (NSString *)clp_stringByStrippingCharactersInSet:(NSCharacterSet *)set
{
return [[self componentsSeparatedByCharactersInSet:set] componentsJoinedByString:@""];
}


- (NSString *)clp_stringByCollapsingWhitespace
{
NSArray *components = [self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
components = [components filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self <> ''"]];


return [components componentsJoinedByString:@" "];
}


@end

在 Swift 中,componentsJoinedByStringjoin(...)替换,因此在这里它只用空格替换非字母数字字符。

let charactersToRemove = NSCharacterSet.alphanumericCharacterSet().invertedSet
let strippedReplacement = " ".join(someString.componentsSeparatedByCharactersInSet(charactersToRemove))

对于 Swift2..。

var enteredByUser = field.text .. or whatever


let unsafeChars = NSCharacterSet.alphanumericCharacterSet().invertedSet


enteredByUser = enteredByUser
.componentsSeparatedByCharactersInSet(unsafeChars)
.joinWithSeparator("")

如果只想删除一个字符,例如删除所有返回值..。

 enteredByUser = enteredByUser
.componentsSeparatedByString("\n")
.joinWithSeparator("")

下面是一个作为扩展的 卡梅伦的类别的 Swift 版本:

extension String {


func stringByStrippingCharactersInSet(set:NSCharacterSet) -> String
{
return (self.componentsSeparatedByCharactersInSet(set) as NSArray).componentsJoinedByString("")
}


func stringByCollapsingWhitespace() -> String
{
var components:NSArray = self.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
let predicate = NSPredicate(format: "self <> ''", argumentArray: nil)
components = components.filteredArrayUsingPredicate(predicate)


return components.componentsJoinedByString(" ")
}
}

我认为,普通循环的执行时间会更快:

@implementation NSString(MyUtil)


- (NSString*) stripNonNumbers {
NSMutableString* res = [NSMutableString new];
//NSCharacterSet *numericSet = [NSCharacterSet decimalDigitCharacterSet];
for ( int i=0; i < self.length; ++i ) {
unichar c = [self characterAtIndex:i];
if ( c >= '0' && c <= '9' ) // this looks cleaner, but a bit slower: [numericSet characterIsMember:c])
[res appendFormat:@"%c", c];
}
return res;
}


@end

Swift 3版本的公认答案:

let unsafeChars = CharacterSet.alphanumerics.inverted
let myStrippedString = myString.components(separatedBy: unsafeChars).joined(separator: "")

这是一个比提供的答案更有效的方法

+ (NSString *)alphanumericString:(NSString *)s {


NSCharacterSet * charactersToRemove = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
NSMutableString * ms = [NSMutableString stringWithCapacity:[s length]];
for (NSInteger i = 0; i < s.length; ++i) {
unichar c = [s characterAtIndex:i];
if (![charactersToRemove characterIsMember:c]) {
[ms appendFormat:@"%c", c];
}
}
return ms;


}

或者作为一个分类

@implementation NSString (Alphanumeric)


- (NSString *)alphanumericString {


NSCharacterSet * charactersToRemove = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
NSMutableString * ms = [NSMutableString stringWithCapacity:[self length]];
for (NSInteger i = 0; i < self.length; ++i) {
unichar c = [self characterAtIndex:i];
if (![charactersToRemove characterIsMember:c]) {
[ms appendFormat:@"%c", c];
}
}
return ms;


}


@end

Swift 5,扩展:

extension String {


/// Will strip all non alpha characters from a string
public var alpha: String {
return components(separatedBy: CharacterSet.alphanumerics.inverted).joined()
}
}