Java implements mobile phone number and ID number**** to hide the middle part

The method used is the replaceAll() method of the Sting class.
1.Mobile phone number

String mobile = "1234567890"
mobile.replaceAll("(\\d{3})\\d{5}(\\d{3})","$1*****$2"));
输出为:123*****890

Among them (\d{3})\d{5}(\d{3}) is the corresponding number of characters for regular matching. $1 and $2 are placeholders corresponding to the values ​​in the two brackets before and after the regular expression.

2. ID number

String indentity = "123456789123456789"
indentity = replaceAll("(\\d{4})\\d{8}(\\w{6})", "$1*****$2"));
输出为:1234*****456789

Among them (\d{4})\d{8}(\w{6}) is the corresponding number of characters for regular matching. $1 and $2 are placeholders corresponding to the values ​​in the two brackets before and after the regular expression.

Guess you like

Origin blog.csdn.net/qq_42182034/article/details/110119856