iOS 删除字符串首尾空格换行

(1)去掉字符串首尾空格的方法:

NSString *str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

(2)去掉字符串首尾换行的方法:

NSString *str = [str stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];

(3)去掉字符串首尾空格和换行的方法:

NSString *str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

(4)替换字符串内所有空格的方法:

NSString *str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];

(5)替换字符串内所有空格的方法:

NSString *str = [str stringByReplacingOccurrencesOfString:@"n" withString:@""];

你可能感兴趣的:(iOS 删除字符串首尾空格换行)