怎么处理,后台返回的 \r \n,字符串自动换行
如果你是放到label里面,当然numberOfLines 是必须大于1的或者等于0;
NSString * str = viewModel.content;
if ([str containsString:@"\\r"]) {
str = [str stringByReplacingOccurrencesOfString:@"\\r" withString:@"\r"];
}
if ([str containsString:@"\\n"]) {
str = [str stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
}
self.label1.numberOfLines = 0;
label.text = str;
就可以解决你的问题了
去除字符串首尾空格、换行
1、去除首尾空格
NSString *title = [title stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
2、去除首尾换行
NSString *title = [title stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
3、去除首尾空格和换行
NSString *title = [title stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
像这样的集合有很多
// 控制字符组
@property (readonly, class, copy) NSCharacterSet *controlCharacterSet;
// 空白字符集
@property (readonly, class, copy) NSCharacterSet *whitespaceCharacterSet;
// 空格和换行字符集
@property (readonly, class, copy) NSCharacterSet *whitespaceAndNewlineCharacterSet;
// 十进制字符集
@property (readonly, class, copy) NSCharacterSet *decimalDigitCharacterSet;
// 字母字符集
@property (readonly, class, copy) NSCharacterSet *letterCharacterSet;
// 小写字母字符集
@property (readonly, class, copy) NSCharacterSet *lowercaseLetterCharacterSet;
// 大写字母字符集
@property (readonly, class, copy) NSCharacterSet *uppercaseLetterCharacterSet;
// 非基本字符集
@property (readonly, class, copy) NSCharacterSet *nonBaseCharacterSet;
// 字母数字字符集
@property (readonly, class, copy) NSCharacterSet *alphanumericCharacterSet;
// 可分解的字符集
@property (readonly, class, copy) NSCharacterSet *decomposableCharacterSet;
// 非法的字符集
@property (readonly, class, copy) NSCharacterSet *illegalCharacterSet;
// 标点符号字符集
@property (readonly, class, copy) NSCharacterSet *punctuationCharacterSet;
// 大写字母字符集
@property (readonly, class, copy) NSCharacterSet *capitalizedLetterCharacterSet;
// 符号字符集
@property (readonly, class, copy) NSCharacterSet *symbolCharacterSet;
// 换行符设置
@property (readonly, class, copy) NSCharacterSet *newlineCharacterSet ;
网友评论