iOS 进阶ios基础知识

iOS开发-NSString去掉所有换行及空格

2018-09-03  本文已影响858人  Pandakingli

1.去掉字符串两端的空格及回车

- (NSString *)removeSpaceAndNewline:(NSString *)str{
    
    NSString *temp = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    
    NSString *text = [temp stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet ]];
    
    return text;
}

2.去掉所有的空格及字符串

- (NSString *)removeSpaceAndNewline:(NSString *)str{
   
    NSString *temp = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
    temp = [temp stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    temp = [temp stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    return temp;
}

上一篇 下一篇

猜你喜欢

热点阅读