本文目录导读:

- 方法一:使用正则表达式 + replaceAll(最简洁)
- 方法二:使用 StringBuilder 手动替换(推荐,性能好)
- 方法三:使用 substring 拼接
- 方法四:使用 String.format(适用于模板化场景)
- 方法五:Apache Commons Lang 工具类(StringUtils.overlay)
- 完整工具类示例(包含空值和长度校验)
- 性能考虑
- 扩展:支持不同长度或规则的隐藏
在Java中隐藏手机号中间位(通常隐藏第4到第7位,138****1234),有几种常见的方法,以下是几种实现方式,从简单到灵活:
使用正则表达式 + replaceAll(最简洁)
public class PhoneNumberMask {
public static void main(String[] args) {
String phone = "13812345678";
// 正则:匹配后3位((\\d{3}))和前4位((\\d{4})),中间替换为****
String masked = phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
System.out.println(masked); // 输出:138****5678
}
}
使用 StringBuilder 手动替换(推荐,性能好)
public class PhoneMaskDemo {
public static String maskPhoneNumber(String phone) {
if (phone == null || phone.length() != 11) {
throw new IllegalArgumentException("手机号必须为11位");
}
StringBuilder sb = new StringBuilder(phone);
// 从索引3开始替换4个字符(第4-7位)
sb.replace(3, 7, "****");
return sb.toString();
}
public static void main(String[] args) {
String result = maskPhoneNumber("13812345678");
System.out.println(result); // 138****5678
}
}
使用 substring 拼接
public class PhoneMaskSubstring {
public static String maskPhone(String phone) {
if (phone == null || phone.length() != 11) {
return phone; // 处理异常情况
}
return phone.substring(0, 3) + "****" + phone.substring(7);
}
public static void main(String[] args) {
System.out.println(maskPhone("13812345678")); // 138****5678
}
}
使用 String.format(适用于模板化场景)
public class PhoneMaskFormat {
public static String maskPhoneFormat(String phone) {
if (phone == null || phone.length() != 11) {
return phone;
}
return String.format("%s****%s", phone.substring(0, 3), phone.substring(7));
}
}
Apache Commons Lang 工具类(StringUtils.overlay)
如果项目中已经引入了 Apache Commons Lang,可以使用:
import org.apache.commons.lang3.StringUtils;
public class PhoneMaskCommons {
public static String maskPhone(String phone) {
// StringUtils.overlay(str, overlay, start, end)
return StringUtils.overlay(phone, "****", 3, 7);
}
}
完整工具类示例(包含空值和长度校验)
public class PhoneMaskUtil {
/**
* 隐藏手机号中间四位
* @param phone 11位手机号
* @return 隐藏后的手机号,如138****1234
*/
public static String maskPhoneNumber(String phone) {
if (phone == null || phone.length() != 11) {
// 根据业务需求决定抛出异常还是返回原字符串
return phone; // 或 throw new IllegalArgumentException("手机号格式不正确");
}
return phone.substring(0, 3) + "****" + phone.substring(7);
}
// 或使用StringBuilder版本
public static String maskPhoneWithBuilder(String phone) {
if (phone == null || phone.length() != 11) {
return phone;
}
StringBuilder sb = new StringBuilder(phone);
sb.replace(3, 7, "****");
return sb.toString();
}
public static void main(String[] args) {
// 测试
System.out.println(maskPhoneNumber("13812345678")); // 138****5678
System.out.println(maskPhoneWithBuilder("13998765432")); // 139****5432
System.out.println(maskPhoneNumber("12345")); // 返回原值或处理异常
}
}
性能考虑
substring方式:性能最好,代码简单直观StringBuilder方式:如果后续还要多次修改字符串,推荐使用- 正则方式:功能强大但性能相对较差,适合处理复杂规则
扩展:支持不同长度或规则的隐藏
如果需要隐藏不同位置或不同字符数,可以通用化:
public static String maskPhoneCustom(String phone, int start, int end, char maskChar) {
if (phone == null || start < 0 || end > phone.length() || start >= end) {
return phone;
}
StringBuilder sb = new StringBuilder(phone);
for (int i = start; i < end; i++) {
sb.setCharAt(i, maskChar);
}
return sb.toString();
}
// 使用
System.out.println(maskPhoneCustom("13812345678", 3, 7, '*')); // 138****5678
System.out.println(maskPhoneCustom("010-12345678", 4, 8, '#')); // 010-####5678
根据你的具体使用场景(是控制台输出、Web展示还是日志脱敏),选择最合适的方法即可。