身份证号码中间某几位数字隐藏

  • Post author:
  • Post category:其他


String pid = "获取的身份证号码";
int hideLength = 8;//替换位数,这里替代中间8位 
int startIndex = pid.length()/2-hideLength/2; 
String replaceSymbol = "*";//替换符号,这里用“*”为例 
StringBuilder stringBuilder = new StringBuilder();
for (int h = 0; h<pid.length();h++){
	char number = pid.charAt(h);
	if (h>=startIndex-1&&h<startIndex+hideLength){
		stringBuilder.append(replaceSymbol);
	}else {
		stringBuilder.append(number);
	}
}
pid = stringBuilder.toString();


出来结果:4102*********62254



版权声明:本文为weixin_42314754原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。