// 将字符串某几位替换为*(格式化银行卡)
public static String replaceText(String str) {
String text = "";
if (str != null && str.length() > 4) {
String front = str.substring(0, str.length() - 4);
String behind = str.substring(str.length() - 4, str.length());
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < front.length(); i++) {
buffer.append("*");
}
buffer = buffer.append(behind);
int index = 0;
//银行卡中间隔几位加空格
while (index < buffer.length()) {
if ((index == 3 || index == 8 || index == 13 || index == 18)) {
buffer.insert(index, ' ');
}
index++;
}
text = buffer.toString();
return text;
} else {
return str;
}
}
转载于:https://my.oschina.net/meetthebetteryour/blog/607798