There are some areas in the project where confidentiality is required:
- The user name part needs to be replaced with * to achieve the confidentiality effect;
- A part of the bank card number needs to be replaced by *
- Part of the ID number needs to be replaced with *
Share this tool class.
1 PackageCom.chinasun;2 3 /**4 * File Description:string is replaced to achieve a confidential effect5 *6 * According to the need to rewrite the following regular is a number, mobile phone number code is not written with the same ID number7 */8 Public classStringreplaceutil {9 /**Ten * According to the different length of the user name, to replace, to achieve a confidential effect One * A * @paramUserName - * User name - * @returnuser name after replacement the */ - Public Staticstring Usernamereplacewithstar (String userName) { -String usernameafterreplaced = ""; - + if(UserName = =NULL) { -UserName = ""; + } A at intNamelength =username.length (); - System.out.println (namelength); - - if(Namelength <= 1) { -usernameafterreplaced = "*"; -}Else if(Namelength = = 2) { inusernameafterreplaced = Replaceaction (UserName, "(? <=\\w{0}) \\w (? =\\w{1})"); -}Else if(Namelength >= 3 && namelength <= 6) { tousernameafterreplaced = Replaceaction (UserName, "(? <=\\d{1}) \\d (? =\\d{1})"); +}Else if(Namelength = = 7) { -usernameafterreplaced = Replaceaction (UserName, "(? <=\\d{1}) \\d (? =\\d{2})"); the}Else if(Namelength = = 8) { *usernameafterreplaced = Replaceaction (UserName, "(? <=\\d{2}) \\d (? =\\d{2})"); $}Else if(Namelength = = 9) {Panax Notoginsengusernameafterreplaced = Replaceaction (UserName, "(? <=\\d{2}) \\d (? =\\d{3})"); -}Else if(Namelength = = 10) { theusernameafterreplaced = Replaceaction (UserName, "(? <=\\d{3}) \\d (? =\\d{3})"); +}Else if(Namelength >= 11) { Ausernameafterreplaced = Replaceaction (UserName, "(? <=\\d{3}) \\d (? =\\d{4})"); the } + - returnusernameafterreplaced; $ $ } - - /** the * Actual replacement action - *Wuyi * @paramusername the * Username - * @paramRegular Wu * Regular - * @return About */ $ Private Staticstring Replaceaction (string username, string regular) { - returnUsername.replaceall (Regular, "*"); - } - A /** + * ID number replacement, keep the top four and last four digits the * - * Returns NULL if the ID number is empty or null, otherwise, returns the replaced string; $ * the * @paramIdcard the * ID Number the * @return the */ - Public Staticstring Idcardreplacewithstar (String idcard) { in the if(Idcard.isempty () | | idcard = =NULL) { the return NULL; About}Else { the returnReplaceaction (Idcard, "(? <=\\d{4}) \\d (? =\\d{4})"); the } the } + - /** the * Bank card replacement, reserved after four digitsBayi * the * Returns null if the bank card number is empty or null, otherwise, returns the replaced string; the * - * @paramBankcard - * Bank card number the * @return the */ the Public Staticstring Bankcardreplacewithstar (String bankcard) { the - if(Bankcard.isempty () | | bankcard = =NULL) { the return NULL; the}Else { the returnReplaceaction (Bankcard, "(? <=\\d{0}) \\d (? =\\d{4})");94 } the } the}
Sensitive information (user name, bank card number, Social Security number, mobile phone number) with asterisks instead