2011-08-30 63 views

回答

2
NSCharacterSet *charactersToRemove = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 "] invertedSet]; 
NSString *stringValueOfTextField = [[searchBar.text componentsSeparatedByCharactersInSet:charactersToRemove] 
            componentsJoinedByString:@""]; 

試試這個: :::

+0

thx,這種污染是非常有益的 –

0
NSRegularExpression *regex = [NSRegularExpression 
     regularExpressionWithPattern:@"\w[[email protected]#$%^&*()_+-={}\[\]:\"|;'\<>?,./]" 
          options:NSRegularExpressionCaseInsensitive 
           error:&error]; 

NSUInteger numberOfMatches = [regex numberOfMatchesInString:string 
                options:0 
                 range:NSMakeRange(0, [string length])]; 

請注意NSRegularExpression僅適用於iOS 4及以上版本。