简体   繁体   English

从 objective-c 中的 NSString 中删除非 ASCII 字符

[英]remove non ASCII characters from NSString in objective-c

I have an app that syncs data from a remote DB that users populate.我有一个应用程序可以从用户填充的远程数据库同步数据。 Seems people copy and paste crap from a ton of different OS's and programs which can cause different hidden non ASCII values to be imported into the system.似乎人们从大量不同的操作系统和程序中复制和粘贴废话,这可能导致将不同的隐藏非 ASCII 值导入系统。

For example I end up with this:例如,我最终得到了这个:

Artist:â â Ioco

This ends up getting sent back into system during sync and my JSON conversion furthers the problem and invalid characters in various places cause my app to crash.这最终在同步期间被发送回系统,我的 JSON 转换进一步加剧了问题,并且各个地方的无效字符导致我的应用程序崩溃。

How do I search for and clean out any of these invalid characters?如何搜索并清除这些无效字符?

While I strongly believe that supporting unicode is the right way to go, here's an example of how you can limit a string to only contain certain characters (in this case ASCII): 虽然我坚信支持unicode是正确的方法,但这里有一个例子,说明如何限制字符串只包含某些字符(在本例中为ASCII):

NSString *test = @"Olé, señor!";

NSMutableString *asciiCharacters = [NSMutableString string];
for (NSInteger i = 32; i < 127; i++)  {
    [asciiCharacters appendFormat:@"%c", i];
}

NSCharacterSet *nonAsciiCharacterSet = [[NSCharacterSet characterSetWithCharactersInString:asciiCharacters] invertedSet];

test = [[test componentsSeparatedByCharactersInSet:nonAsciiCharacterSet] componentsJoinedByString:@""];

NSLog(@"%@", test); // Prints @"Ol, seor!"

A simpler version of Morten Fast's answer: Morten Fast 答案的简单版本:

NSString *test = @"Olé, señor!";

NSCharacterSet *nonAsciiCharacterSet = [[NSCharacterSet 
           characterSetWithRange:NSMakeRange(32, 127 - 32)] invertedSet];

test = [[test componentsSeparatedByCharactersInSet:nonAsciiCharacterSet] 
                          componentsJoinedByString:@""];

NSLog(@"%@", test); // Prints @"Ol, seor!"

Notably, this uses NSCharacterSet 's +characterSetWithRange: method to simply specify the desired ASCII range rather than having to create a string, etc.值得注意的是,这使用NSCharacterSet+characterSetWithRange:方法来简单地指定所需的 ASCII 范围,而不必创建字符串等。

The results are identical, as comparing one to the other with isEqual: returns YES .结果是相同的,因为使用isEqual:将一个与另一个进行比较返回YES

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM