去空格 whitespaceAndNewlineCharacterSet和過濾字串

來源:互聯網
上載者:User

標籤:using   com   end   包括   定義   format   buffer   需要   空格   

 一、過濾字串

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

 

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

NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"@/:;()¥「」"、[]{}#%-*+=_//|~<>$€^?‘@#$%^&*()_+‘/"""];由於NSString中有全形符號和半形符號, 因此有些符號要包括全形和半形的然後調用stringByTrimmingCharactersInSetNSString *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和過濾字串

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.