iOS 銀行卡號密文形式展示
阿新 • • 發佈:2019-02-14
#import <Foundation/Foundation.h> @interface BankModel : NSObject @property(nonatomic,assign) NSInteger bankId; @property(nonatomic,copy) NSString* bankImg; @property(nonatomic,copy) NSString* bankName; @property(nonatomic,copy) NSString*progressQueryUrl; @property(nonatomic,assign) NSInteger*bankCardInformationId; @property(nonatomic,copy) NSString*cardHolder; @property(nonatomic,copy) NSString*bankCardNumber; @property(nonatomic,copy) NSString*reservePhoneNumber; @property(nonatomic,copy) NSString*bankCardType; @property(nonatomic,copy) NSString* bankIcon; - (NSString*)getNewStarBankNumWitOldNum; @end
#import "BankModel.h" @implementation BankModel -(NSString *)getNewStarBankNumWitOldNum{ NSString *bankNum = self.bankCardNumber; NSMutableString *mutableStr; if (bankNum.length) { mutableStr = [NSMutableString stringWithString:bankNum]; for (int i = 0 ; i < mutableStr.length; i ++) { if (i>3&&i<mutableStr.length - 4) { [mutableStr replaceCharactersInRange:NSMakeRange(i, 1) withString:@"*"]; } } NSString *text = mutableStr; NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"]; text = [text stringByReplacingOccurrencesOfString:@" " withString:@""]; NSString *newString = @""; while (text.length > 0) { NSString *subString = [text substringToIndex:MIN(text.length, 4)]; newString = [newString stringByAppendingString:subString]; if (subString.length == 4) { newString = [newString stringByAppendingString:@" "]; } text = [text substringFromIndex:MIN(text.length, 4)]; } newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]]; return newString; } return bankNum; } @end