ios 字符串中去掉空格和换行符

ios中去掉字符串中的空格和换行符分为两种情况,第一种情况是去除字符串首尾的空格和换行符,第二种情况是去除字符串中出现的空格和换行符。代码如下;

<span style="font-size:18px;">    // 去掉字符串中的空格和换行符
    str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@"\r\n" withString:@""]; //换行符是\r\n 而不是\n
    NSLog(@"str:%@",str);
    
    // 去掉字符串首尾的空格和字符串
    str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSLog(@"_______str:%@",str);</span>


猜你喜欢

转载自blog.csdn.net/fenliuheliu/article/details/42172205