Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将电子邮件中的前3个字符屏蔽为“*&引用;_C#_String - Fatal编程技术网

C# 将电子邮件中的前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

我想用“*”来屏蔽电子邮件的前3个字符

例如

谢谢

试试:

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      
    */
    }