NSString *str = @" xxxx ";
// 1. 去掉首尾空格和换行符
str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
// 2. 去掉所有空格和换行符
str = [str stringByReplacingOccurrencesOfString:@"\r" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@""];
转自:https://blog.csdn.net/saw471/article/details/52778411