15 objective-c ios
我将段落转换为包含许多特殊字符的单词
" , " . `
Run Code Online (Sandbox Code Playgroud)
如何在nsstring中删除此字符并在nsstring中只获取字母
前"新"到新的 //the special characters are change
Mad*_*huP 77
NSString *unfilteredString = @"!@#$%^&*()_+|abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
NSCharacterSet *notAllowedChars = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"] invertedSet];
NSString *resultString = [[unfilteredString componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""];
NSLog (@"Result: %@", resultString);
Run Code Online (Sandbox Code Playgroud)
尝试这可能有助于你
有很多方法可以解决这个问题.例如,这是使用正则表达式的解决方案.这只是一个例子.我们不知道您要删除的所有特殊字符.
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:@"[,\\.`\"]"
options:0
error:NULL];
NSString *sampleString = @"The \"new\" quick brown fox, who jumped over the lazy dog.";
NSString *cleanedString = [expression stringByReplacingMatchesInString:sampleString
options:0
range:NSMakeRange(0, sampleString.length)
withTemplate:@""];
printf("cleaned = %s",[cleanedString UTF8String] );
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31448 次 |
| 最近记录: |