嗨,朋友们,我想在表格视图单元格中显示信用卡号,所以我只想显示最后四个字符,例如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;


但它不能正常工作,你可以建议任何参考或代码...

最佳答案

用这个

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);

10-05 17:43