在iOS中 ��可以使用stringByTrimmingCharactersInSet函数过滤字符串中的特殊符号。
1、
首先自己定义一个NSCharacterSet, 包含需要去除的特殊符号
NSCharacterSet set1 = [NSCharacterSet characterSetWithCharactersInString:@"@/:;()¥「」"、[]{}#%-+=\|~<>$€•'@#$%&*()+'""];
由于NSString中有全角符号和半角符号, 因此有些符号要包括全角和半角的
然后调用stringByTrimmingCharactersInSet
NSString *trimmedString1 = [string stringByTrimmingCharactersInSet:set1];
trimmedString1就是过滤后的字符串
2、去除空格
NSCharacterSet *set2 = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSString *trimmedString2 = [string stringByTrimmingCharactersInSet:set2];
3、
字符串花样处理:NSCharacterSet
4.去除字符串内任何字符
Remove Any Character
NSString *str = @"430F7ABD-DB61-4247-B716-FCE34C0F56C6";
// Create character set with specified characters
NSMutableCharacterSet *characterSet =
[NSMutableCharacterSet characterSetWithCharactersInString:@"0-"];
[characterSet formUnionWithCharacterSet:[NSCharacterSet letterCharacterSet]];
// Build array of components using specified characters as separtors
NSArray *arrayOfComponents = [str componentsSeparatedByCharactersInSet:characterSet];
// Create string from the array components
NSString *strOutput = [arrayOfComponents componentsJoinedByString:@""];
NSLog(@"New string: %@", strOutput);
#---成功去除了所有的 - 0 d
终端打印:43761424771634566