Sensitive information (user name, bank card number, Social Security number, mobile phone number) with asterisks instead

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.