public class Test {
public static void main(String[] args) {
String str = "13800138000";
for(int i = -20; i < 20; i++) {
System.out.println(i + " --> " + asteriskHidden(str, i));
}
}
public static String asteriskHidden(String str, int count) {
return hidden(str, count, '*');
}
public static String hidden(String str, int count, char replacement) {
if(str == null || count < 1) {
return str;
}
char[] chs = str.toCharArray();
int offset = 0;
if(chs.length - count > 0) {
offset = (chs.length - count) / 2;
}
int end = Math.min(offset + count, chs.length);
while(offset < end) {
chs[offset++] = '*';
}
return new String(chs);
}
}
隐藏手机号中间特定位数
最新推荐文章于 2024-08-22 23:16:45 发布