NSString *text = self.model.orderNo;
///首先定义一个0123456789\b的字符集
NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"];
///去掉字符中的空格
text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *newString = @"";
while (text.length > 0) {
///每4位截取下
NSString *subString = [text substringToIndex:MIN(text.length, 4)];
newString = [newString stringByAppendingString:subString];
if (subString.length == 4) {
//当字符长度为4的时候拼接上空格
newString = [newString stringByAppendingString:@" "];
}
text = [text substringFromIndex:MIN(text.length, 4)];
}
///去掉除字符集以外的其他字符
newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];
self.orderNoLabel.text = newString;
网友评论