Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 343
  • Last Modified:

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
0
newone2011
Asked:
newone2011
1 Solution
 
a_bCommented:
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now