Making a credit card no

I need to mask middle 6 digits of CC no which can be of length 16 or 15. Please confirm if my method id correct:
For example
5410102534567890
becomes
541010******7890

500120013001100
becomes
500120******100

public static String maskCCNo(String ccno){
        int cclen = ccno.length();
        int doNotMaskStartLen = 6;
        int doNotMaskEndLen =  4;
        int masklen = cclen-(doNotMaskStartLen+doNotMaskEndLen) ;
        StringBuffer maskedBuf = new StringBuffer(ccnum.substring(0,startlen));
        for(int i=0;i<masklen;i++) {
            maskedBuf.append('*');
        }
        maskedbuf.append(ccnum.substring(startlen+masklen, cclen));
        String maskCC = maskedbuf.toString();
        return maskCC;
    }

5410102534567890

541010******7890

500120013001100
500120******100
newone2011Asked:
Who is Participating?
 
a_bConnect With a Mentor Commented:
String creditCardNum = "5410102534567890";
String  masked = creditCardNum.subStr(0,5) + "******" + creditCardNum.subStr(11,creditCardNum.length()-1);

This will work for both 15 digits and 16 digits. It's a simple one liner instead of using loops.
0
 
newone2011Author Commented:
k, let me test this
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.