一,代码。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. //替换回车符和空格
NSString *oldStr=@" 1 2 4 ";
NSLog(@"----oldStr---%@",oldStr);
//去掉首尾空格
NSString *newStr=[oldStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@"---newStr---%@",newStr);
//去掉中间的空格
NSString *newNewStr=[newStr stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"---newNewStr--%@",newNewStr);
}
二,输出。
2015-10-23 13:16:50.310 字符串替换回车和换行[7678:198286] ----oldStr--- 1 2 4
2015-10-23 13:16:50.311 字符串替换回车和换行[7678:198286] ---newStr---1 2 4
2015-10-23 13:16:50.311 字符串替换回车和换行[7678:198286] ---newNewStr--