嗨朋友我想在表格视图单元格中显示信用卡号码,所以我想只显示最后四个字符,如 XXXXXXXXXX1111 第一个字符用x或其他字符替换我怎么能这样做谁能帮我 我试过像吼叫一样
NSString *CardNumber = [CardNumberValues objectAtIndex:indexPath.row];
NSRange subStringRange = NSMakeRange(0,[CardNumber length]-4);
NSMutableCharacterSet *NumSet = [NSMutableCharacterSet characterSetWithRange:subStringRange];
CardNumber = [[CardNumber componentsSeparatedByCharactersInSet:NumSet] componentsJoinedByString:@"X"];
cell.CardNumber.text = CardNumber;
但它不起作用你可以建议任何参考或代码......
答案 0 :(得分:7)
使用此
NSString *CardNumber = @"12345678901234";
NSString *str_padding=@"";
NSRange subStringRange = NSMakeRange(0,[CardNumber length]-4);
str_padding =[str_padding stringByPaddingToLength:subStringRange.length withString:@"X" startingAtIndex:0];
CardNumber = [CardNumber stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@",[CardNumber substringToIndex:subStringRange.length]] withString:str_padding];
NSLog(@"%@",CardNumber);
答案 1 :(得分:0)
NSString *cardNumber = [NSString stringWithFormat: @"XXXX-XXXX-XXXX-%@", [[CardNumberValues objectAtIndex:indexPath.row] subStringWithIndex:12]];
cell.CardNumber.text = cardNumber;
我假设卡号是16位数。