码迷,mamicode.com
首页 > 其他好文 > 详细

去空格 whitespaceAndNewlineCharacterSet和过滤字符串

时间:2016-12-18 12:11:20      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:using   com   end   包括   定义   format   buffer   需要   空格   

 一、过滤字符串

  可以使用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"]]);

 

去空格 whitespaceAndNewlineCharacterSet和过滤字符串

标签:using   com   end   包括   定义   format   buffer   需要   空格   

原文地址:http://www.cnblogs.com/mkai/p/6193972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!