Recently done a functional module on the bank, the need to use the middle portion of the Privacy Information bank to replace, so write down, the following code:
<summary>////To replace the passed-in middle part character with a special character///</summary>//<param name= "value" & gt; string to be replaced </param>//<param name= "Startlen" > Pre-reserved length </param>//<param name= "Endlen" &G t; tail retention length </param>//<param name= "Replacechar" > Special characters </param>//<returns> strings replaced by special characters & lt;/returns> private static string Replacewithspecialchar (string value, int startlen = 4, int endlen = 4, char s Pecialchar = ' * ') {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 invocation and effect are as follows:
Replacewithspecialchar ("Collian Stay", 1, 0, ' * ')-->result: Ke * stay
Replacewithspecialchar ("622212345678485")-->result:6222*******8485
Replacewithspecialchar ("622212345678485", 4, 4, ' * ')-->result:6222*******8485
Note: If the incoming Startlen/endlen exceeds the string length, an subscript out-of-bounds exception will be thrown
Just began to write blog, presentation, naming norms, technical content are still lacking, I hope you have more guidance, thank you!!!
C # Privacy Information (bank account, ID number, name) middle part special character substitution (*)