去空格 whitespaceAndNewlineCharacterSet和過濾字符串


 一、過濾字符串

  可以使用stringByTrimmingCharactersInSet函數過濾字符串中的特殊符號

 

  首先自己定義一個NSCharacterSet, 包含需要去除的特殊符號

NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"@/:;()¥「」"、[]{}#%-*+=_//|~<>$€^•'@#$%^&*()_+'/"""];



由於NSString中有全角符號和半角符號, 因此有些符號要包括全角和半角的



然后調用stringByTrimmingCharactersInSet



NSString *trimmedString = [string stringByTrimmingCharactersInSet:set];



trimmedString就是過濾后的字符串

二、去除空格

  1.去掉兩端的空格

  1 [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]   

 

  2.去掉多余的空格

1 NSString *str = @"    this     is a    test    .   ";  
2       
3     NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];  
4     NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];  
5       
6     NSArray *parts = [str componentsSeparatedByCharactersInSet:whitespaces];  
7     NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];  
8     str = [filteredArray componentsJoinedByString:@" "]; 

 

  3.去掉所有空格

 1 [str stringByReplacingOccurrencesOfString:@" " withString:@""]  

 

  4.去掉最左邊的空格  和  去掉最右邊的空格

@interface NSString (TrimmingAdditions)  
- (NSString *)stringByTrimmingLeftCharactersInSet:(NSCharacterSet *)characterSet ;  
- (NSString *)stringByTrimmingRightCharactersInSet:(NSCharacterSet *)characterSet ;  
@end  
  
@implementation NSString (TrimmingAdditions)  
  
- (NSString *)stringByTrimmingLeftCharactersInSet:(NSCharacterSet *)characterSet {  
    NSUInteger location = 0;  
    NSUInteger length = [self length];  
    unichar charBuffer[length];      
    [self getCharacters:charBuffer];  
  
    for (location; location < length; location++) {  
        if (![characterSet characterIsMember:charBuffer[location]]) {  
            break;  
        }  
    }  
  
    return [self substringWithRange:NSMakeRange(location, length - location)];  
}  
  
- (NSString *)stringByTrimmingRightCharactersInSet:(NSCharacterSet *)characterSet {  
    NSUInteger location = 0;  
    NSUInteger length = [self length];  
    unichar charBuffer[length];      
    [self getCharacters:charBuffer];  
  
    for (length; length > 0; length--) {  
        if (![characterSet characterIsMember:charBuffer[length - 1]]) {  
            break;  
        }  
    }  
  
    return [self substringWithRange:NSMakeRange(location, length - location)];  
}  
  
@end  

    例如:NSLog(@"%@",[@"abc 123 " stringByTrimmingRightCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]);

      :NSLog(@"%@",[@"0.012300" stringByTrimmingRightCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"0"]]);

 

 一個非常好的例子,來源於http://nshipster.com/nscharacterset/, 去掉多余的空格(包括兩端的和中間的)

NSString *exampleStr = @" My name    is Johnny!";  
exampleStr = [exampleStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];  
NSArray *exampleArr = [exampleStr componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];  
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self <> ''"];  
exampleArr = [exampleArr filteredArrayUsingPredicate:predicate];  
exampleStr = [exampleArr componentsJoinedByString:@" "];

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM