一,代碼。
- (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--124