NSString处理技巧

使用场景举例:可以用在处理用户用户输入在UITextField的文本

//待处理的字符串

NSString *string = @" A B  CD   EFG\n MN\n";

//字符串替换,处理后的string1 = @"ABCDEF\nMN\n";

NSString *string1 = [string stringByReplacingOccurrencesOfString:@" " withString:@""];

//去除两端空格(注意是两端),处理后的string2 = @"A B  CD   EFG\n MN\n";

NSString *string2 = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

//去除两端回车(注意是两端),处理后的string3 = @" A B  CD   EFG\n MN";

NSString *string3 = [string stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];

//去除两端空格和回车(注意是两端),处理后的string4 = @"A B  CD   EFG\n MN";

NSString *string4 = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

你可能感兴趣的:(NSString处理技巧)