ios 删除 NSString 中的特殊字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13430361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove Special Characters in NSString
提问by MadhuP
i am convert paragraph into words it contains many special characters like
我正在将段落转换为包含许多特殊字符的单词,例如
" , " . `
how to remove this characters in nsstring and get only alphabets in nsstring
如何删除 nsstring 中的这些字符并仅获取 nsstring 中的字母
ex
"new" to new //the special characters are change
从“新”到新 //the special characters are change
回答by MadhuP
NSString *unfilteredString = @"!@#$%^&*()_+|abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
NSCharacterSet *notAllowedChars = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"] invertedSet];
NSString *resultString = [[unfilteredString componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""];
NSLog (@"Result: %@", resultString);
TRY THIS IT MAY HELPS YOU
试试这个它可能会帮助你
回答by FluffulousChimp
There are numerous ways of dealing with this. As an example, here's a solution using regular expressions. This is just an example. We don't know the entire range of special characters that you want to remove.
有很多方法可以解决这个问题。例如,这是使用正则表达式的解决方案。这只是一个例子。我们不知道您要删除的所有特殊字符。
#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;
}
回答by green0range
Per comment of @Rostyslav Druzhchenko from on the selected answer of @MadhuP:
根据@Rostyslav Druzhchenko 对@MadhuP 所选答案的评论:
NSString *unfilteredString = @"!@#$%^&*()_+|abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
NSCharacterSet *notAllowedChars = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
NSString *escapedString = [[unfilteredString componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""];
NSLog (@"Result: %@", escapedString);
This is the answer that will use alphanumericCharacterSet
to handle multiple countries character set.
这是将用于alphanumericCharacterSet
处理多个国家/地区字符集的答案。
回答by Andrea
Accepted answer in SWIFT (but not SWIFTY way):
SWIFT 中接受的答案(但不是 SWIFTY 方式):
let notAllowedCharactersSet = NSCharacterSet(charactersInString: "ABCDEFGHIJKLMNOPQRSTUVWXYZ").invertedSet
let filtered = (stringToFilter.componentsSeparatedByCharactersInSet(notAllowedCharactersSet) as NSArray).componentsJoinedByString("")
回答by dstefanis
I'm sure there's a more elegant solution, but for anyone trying to do this in Swift, here's what I did to make sure there were no special characters in my users' phone numbers.
我确信有一个更优雅的解决方案,但对于尝试在 Swift 中执行此操作的任何人,以下是我为确保用户的电话号码中没有特殊字符所做的工作。
var phone = "+1 (555) 555 - 5555"
var removeChars: NSCharacterSet = NSCharacterSet(charactersInString: "1234567890").invertedSet
var charArray = phone.componentsSeparatedByCharactersInSet(removeChars)
var placeholderString = ""
var formattedPhoneNumber: String = placeholderString.join(charArray).stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
The stringByTrimmingCharactersInSet
might not be necessary.
在stringByTrimmingCharactersInSet
可能没有必要。