class Program { static void Main(string[] args) { string str = ReplaceWithSpecialChar("221282188801019662", 4, 4); Console.WriteLine(str); Console.ReadKey(); } /// <summary> /// 将传入的字符串中间部分字符替换成特殊字符 /// </summary> /// <param name="value">需要替换的字符串</param> /// <param name="startLen">前保留长度</param> /// <param name="endLen">尾保留长度</param> /// <param name="replaceChar">特殊字符</param> /// <returns>被特殊字符替换的字符串</returns> private static string ReplaceWithSpecialChar(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; } }
原文:http://www.cnblogs.com/qzxj/p/7140471.html