In Java, I have to wrap a string value to another using RegEx and function replace.
Example #1: What will replace "123C5" (5 characters) with "*****"?
Example #2: What will replace "12354CF5214" (11 characters) with "*******5214"?
replace only the first 7 characters
Currently, i am using this function but i need to optimize it :
public String getMaskedValue(String value) {
String maskedRes = "";
if (value!=null && !value.isEmpty()) {
maskedRes = value.trim();
if (maskedRes.length() <= 5) {
return maskedRes.replaceAll(value, "$1*");
} else if (value.length() == 11) {
return maskedRes.replace(value.substring(0, 7), "$1*");
}
}
return maskedRes;
}
can anyone help me please ? thank you for advanced