C# 将电子邮件中的前3个字符屏蔽为“*&引用;
我想用“*”来屏蔽电子邮件的前3个字符 例如 谢谢试试:C# 将电子邮件中的前3个字符屏蔽为“*&引用;,c#,string,C#,String,我想用“*”来屏蔽电子邮件的前3个字符 例如 谢谢试试: int maskCount = 3; (new String('*', maskCount)) + a.Substring(maskCount) 或 这可以通过以下方式完成: 将字符串复制到新变量(假设您希望保留完整的电子邮件地址以备将来使用), 在新字符串的开头有3个*,并且 使用string.Substring(n)获取原始字符串的其余部分 将其放入函数: private string MaskString(string strin
int maskCount = 3;
(new String('*', maskCount)) + a.Substring(maskCount)
或
这可以通过以下方式完成: 将字符串复制到新变量(假设您希望保留完整的电子邮件地址以备将来使用), 在新字符串的开头有3个*,并且 使用
string.Substring(n)
获取原始字符串的其余部分
将其放入函数:
private string MaskString(string stringToMask, int charsToMask, char maskingChar = '*')
{
string returnString = "";
//Check the length of the string is larger than the chars to mask
if(stringToMask.Length < charsToMask)
{
throw new ArgumentException(
string.Format(
"The number of characters to mask is more than the lenght of the string!\ncharsToMask: {0}\nlength of string: {1}",
charsToMask,
stringToMask.Length
)
);
}
//add the masking char the required number of times
for(int i = 0; i < charsToMask; i++)
{
returnString += maskingChar;
}
//only add the remaining chars of the original string if the length is less than the no. of chars to mask
if(stringToMask.Length > charsToMask)
returnString += stringToMask.Substring(charsToMask);
return returnString;
}
a=“***”+a.子字符串……a=“***”+string.Concat(a.Skip(3))代码>
a.Substring(maskCount).PadLeft(a.Length, '*')
private string MaskString(string stringToMask, int charsToMask, char maskingChar = '*')
{
string returnString = "";
//Check the length of the string is larger than the chars to mask
if(stringToMask.Length < charsToMask)
{
throw new ArgumentException(
string.Format(
"The number of characters to mask is more than the lenght of the string!\ncharsToMask: {0}\nlength of string: {1}",
charsToMask,
stringToMask.Length
)
);
}
//add the masking char the required number of times
for(int i = 0; i < charsToMask; i++)
{
returnString += maskingChar;
}
//only add the remaining chars of the original string if the length is less than the no. of chars to mask
if(stringToMask.Length > charsToMask)
returnString += stringToMask.Substring(charsToMask);
return returnString;
}
public void TestMasking()
{
string a = "abcdef@email.com";
Console.WriteLine("a: {0}", a);
Console.WriteLine("Masked a: {0}", MaskString(a, 3));
/*Output:
a: abcdef@email.com
Masked a: ***def@email.com
*/
Console.WriteLine("a: {0}", a);
Console.WriteLine("Masked a: {0}", MaskString(a, 5));
/*Output:
a: abcdef@email.com
Masked a: *****f@email.com
*/
}