开发者

Substring with asterisks in NSString

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];
NSRan开发者_开发百科ge 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...


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜