C # replacement of special characters in the middle of privacy information (bank account, ID card number, name (*),

Source: Internet
Author: User

C # replacement of special characters in the middle of privacy information (bank account, ID card number, name (*),

Recently, a function module about banks needs to replace the middle part of the bank account with *, so I wrote the following code:

/// <Summary> /// Replace the middle part of the input string with special characters /// </summary> /// <param name = "value"> replace </param> /// <param name = "startLen"> prefix length </param> /// <param name = "endLen"> suffix length </ param> /// <param name = "replaceChar"> special character </param> // <returns> string replaced by special characters </returns> private static string replacewitheat ecialchar (string value, int startLen = 4, int endLen = 4, char specialChar = '*') {try {int lenth = value. length-startLen-endLen; string replaceStr = value. substring (startLen, lenth); string specialStr = string. empty; for (int I = 0; I <replaceStr. length; I ++) {specialStr + = specialChar;} value = value. replace (replaceStr, specialStr);} catch (Exception) {throw;} return value ;}

  

The call and effect are as follows:

ReplaceWithSpecialChar ("Ke xiaodan", 1, 0, '*') --> Result: Ke * Stay

ReplaceWithSpecialChar ("622212345678485") --> Result: 6222 ******** 8485

ReplaceWithSpecialChar ("622212345678485", 4, 4, '*') --> Result: 6222 ******** 8485

 

Note: If the input startLen/endLen exceeds the string length, a subscript out-of-bounds exception is thrown.

I started to write a blog. The expressions, naming rules, and technical content are still insufficient. I hope you can give me some advice. Thank you !!!

 

Related Article

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.