银行卡号加入* 并四个一个空格

+(NSString *)getNewBankNumWitOldBankNum:(NSString *)bankNum

{

    NSMutableString *mutableStr;

    if (bankNum.length) {

    mutableStr = [NSMutableString stringWithString:bankNum];

//如果不需要*号把下面for循环注了即可

    for (int i = 0 ; i < mutableStr.length; i ++) {

        if (i>2&&i

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


}

你可能感兴趣的:(银行卡号加入* 并四个一个空格)