How to mask a String while showing part of the String (last 4 digits)
Apr 26, 2010
0
comments
Following code sample shows how to mask a string while showing last 4 digits. Normally you are not allwoed to save the entire account number or credit card number in your database. Better option is to mask the number and saving.
Masked String : xxxxxxxxxxxx1221
Here a link that describes why account number should be masked.
http://www.pinoytechblog.com/archives/does-your-bank-mask-your-account-number
public class Mask {
public static void main(String[] args) {
String accno = "4444333322221221";
StringBuffer sb = new StringBuffer();
if (accno != null && !accno.isEmpty()) {
Integer length = accno.length();
Integer mask = length;
if (length > 4) {
mask = length - 4;
for (int i = 0; i < mask; i++) {
sb.append("x");
}
sb.append(accno.substring(mask));
}
System.out.println("Masked String : " + sb);
}
}
}
result wil be shown like this:Masked String : xxxxxxxxxxxx1221
Here a link that describes why account number should be masked.
http://www.pinoytechblog.com/archives/does-your-bank-mask-your-account-number
Labels:



