Last active
September 4, 2025 03:32
-
-
Save donma/9db0c0e07684a3bab67a5d91dc0253a3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// 遮罩名字 | |
/// </summary> | |
/// <param name="name">原始姓名</param> | |
/// <param name="keepStart">保留開頭幾個字</param> | |
/// <param name="keepEnd">保留結尾幾個字</param> | |
/// <returns>遮罩後的姓名</returns> | |
public static string MaskName(string name, int keepStart = 1, int keepEnd = 1) | |
{ | |
if (string.IsNullOrEmpty(name)) return name; | |
int length = name.Length; | |
// 如果總長度 <= 要保留的字數,不做遮罩 | |
if (length <= keepStart + keepEnd) return name; | |
// 中間需要遮罩的字數 | |
int maskCount = length - keepStart - keepEnd; | |
string mask = new string('●', maskCount); | |
// 拼接 | |
return name.Substring(0, keepStart) + mask + name.Substring(length - keepEnd, keepEnd); | |
} | |
//Result: | |
//MaskName("許當麻"); // 結果:許●麻 | |
//MaskName("當麻"); // 結果:當● | |
//MaskName("許當麻",2,0) // 結果: 許當● | |
//MaskName("當麻", 0, 1) // 結果: ●麻 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment