zoukankan      html  css  js  c++  java
  • 去空格

    1.去掉两端的空格

    1. [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]   

    2.去掉多余的空格

     
    1. NSString *str = @"    this     is a    test    .   ";  
    2.       
    3.     NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet]; //获取空格字符       
    4.     NSArray *parts = [str componentsSeparatedByCharactersInSet:whitespaces];  
    5. //等同于NSArray *parts = [str componentsSeparatedByString:@" "];
    6.     NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];  
    7.     NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];  
    8.     str = [filteredArray componentsJoinedByString:@" "];  

    3.去掉所有空格

      1. [str stringByReplacingOccurrencesOfString:@" " withString:@""]  
  • 相关阅读:
    10.31JS日记
    10.24JS日记
    10.23JS日记
    10.22JS日记
    10.19JS日记
    10.18JS日记
    Tomcat—Bad Request
    2016年上半年总结
    线程间操作无效
    压缩字符串的函数
  • 原文地址:https://www.cnblogs.com/swallow37/p/5007212.html
Copyright © 2011-2022 走看看