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 !!!