NSString 中带星号的子字符串

发布于 2024-12-05 04:34:17 字数 549 浏览 0 评论 0原文

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

但它不起作用,你可以建议任何参考或代码吗...

Hi friends i want to display credit card number in table view cell,so i want to display only last four characters like XXXXXXXXXX1111 first charecters is replaced with x or some other character how can i do this can any one help me

i have tried like bellow

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;

but its not working can u suggest any reference or code...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猥琐帝 2024-12-12 04:34:17

用这个

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

Use this

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);
故乡的云 2024-12-12 04:34:17
NSString *cardNumber = [NSString stringWithFormat: @"XXXX-XXXX-XXXX-%@", [[CardNumberValues objectAtIndex:indexPath.row] subStringWithIndex:12]];
cell.CardNumber.text = cardNumber;

我假设卡号是 16 位数字。

NSString *cardNumber = [NSString stringWithFormat: @"XXXX-XXXX-XXXX-%@", [[CardNumberValues objectAtIndex:indexPath.row] subStringWithIndex:12]];
cell.CardNumber.text = cardNumber;

I am assuming that card numbers are 16 digits.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文