用java写一个从字符串中找出固定电话,并对固定电话进行脱敏处理的算法
时间: 2024-03-23 20:36:54 浏览: 15
可以使用正则表达式来匹配固定电话,然后再进行脱敏处理。以下是一个Java代码示例:
```java
import java.util.regex.*;
public class Main {
public static void main(String[] args) {
String input = "我的电话是010-12345678,公司电话是021-87654321。";
Pattern pattern = Pattern.compile("\\d{3}-\\d{8}|\\d{4}-\\d{7}"); // 匹配固定电话的正则表达式
Matcher matcher = pattern.matcher(input);
StringBuffer output = new StringBuffer();
while (matcher.find()) {
String phone = matcher.group();
phone = phone.substring(0, 3) + "****" + phone.substring(7); // 脱敏处理
matcher.appendReplacement(output, phone);
}
matcher.appendTail(output);
System.out.println(output.toString());
}
}
```
输出结果为:
```
我的电话是010-****5678,公司电话是021-****4321。
```