如何在第一个字符后屏蔽字符串

How to mask string after first character

假设我有一个由人名组成的字符串:

var name = "Jason, Bruno Mars";

如何使用逗号后面的名称 X 和 returns 屏蔽 string

var name = "Jason, BXXXX MXXX";

我尝试过使用以下方法,但只有前面的字符被屏蔽:

string first, second, output;
bool hasSpace, hasComma;
int int_LENGTH;
var name = "Jason, Bruno Mars";
hasComma = name.Contains(",");

if (hasComma == true)
{
    int_LENGTH = name.IndexOf(",");
    if (int_LENGTH > 0)
    {
        first = name.Substring(0, int_LENGTH);
    }

    second = string.Join(",", name.Split(" ").Skip(1));
    hasSpace = second.Contains(" ");

    if (hasSpace == true)
    {
        second = string.Concat(new string('X', 12), second.Substring(second.Length - 4));
        output = first + "," + second;
    }
}

任何人都知道如何以更有效的方式实现相同的结果?

private string MaskName(string name)
{
    var parts = name.Split(',');
    var subparts = parts[1].Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);

    for (var i = 0; i < subparts.Length; i++)
    {
        var subpart = subparts[i];

        subparts[i] = subpart[0] + new string('X', subpart.Length - 1);
    }

    return parts[0] + ", " + string.Join(" ", subparts);
}
static string MaskName(string name)
    {
        string maskedString = string.Empty;

        string[] names = name.Split(',');
        if (names.Length > 0)
        {
            maskedString = names[0] + ",";
        }
        if (names.Length > 1)
        {
            string[] arrName = names[1].Split(' ');
            foreach (string s in arrName)
            {
                if (s.Length > 0)
                    maskedString += " " + s[0].ToString().PadRight(s.Length, 'X');
            }
        }
        return maskedString;
    }
Using This code..

    static string MaskName(string name)
    {
        string maskedString = string.Empty;

        string[] names = name.Split(',');
        if (names.Length > 0)
        {
            maskedString = names[0] + ",";
        }
        if (names.Length > 1)
        {
            string[] arrName = names[1].Split(' ');
            foreach (string s in arrName)
            {
                if (s.Length > 0)
                    maskedString += " " + s[0].ToString().PadRight(s.Length, 'X');
            }
        }
        return maskedString;
    }

输出:-

试试这个

 private string ReturnMaskedName(string name)
        {
            string temporary = "";
            var newname = (name.Split(new string[] { "," }, StringSplitOptions.None)[1].Trim().Split(new String[] { " " }, StringSplitOptions.None));

            foreach (string allnames in newname)
            {
                temporary = temporary + " " + allnames[0].ToString() + new string('X', allnames.Length - 1);
            }
            return name.Split(new string[] { " " }, StringSplitOptions.None)[0] + " " + temporary;
        }

不使用 SplitRegexusing System.Linq 的有效掩码方法:

对于 C# 版本 < 7.2:

static string MaskName(string name)
{
    int indexOfComma = name.IndexOf(',');

    if (indexOfComma != -1)
    {
        char[] temp = name.ToCharArray();

        bool isFirstChar = true;

        for (int i = indexOfComma + 1; i < temp.Length; i++)
        {
            if (temp[i] == ' ')
                isFirstChar = true;
            else if (isFirstChar)
                isFirstChar = false;
            else
                temp[i] = 'X';
        }

        return new string(temp);
    }
    else
    {
        return name;
    }
}

对于 C# 版本 >= 7.2:

static string MaskName(ReadOnlySpan<char> name)
{
    int indexOfComma = name.IndexOf(',');

    if (indexOfComma != -1)
    {
        Span<char> temp = stackalloc char[name.Length];

        name.CopyTo(temp);

        bool isFirstChar = true;

        for (int i = indexOfComma + 1; i < temp.Length; i++)
        {
            if (temp[i] == ' ')
                isFirstChar = true;
            else if (isFirstChar)
                isFirstChar = false;
            else
                temp[i] = 'X';
        }

        return new string(temp);
    }
    else
    {
        return name.ToString();
    }
}

另一个选项,使用 Regex.Matches 到 select 除第一个字母之外的名称部分。正则表达式收集所有由 space 分隔的字符串部分,跳过逗号之前的内容。
然后将 Matches 的集合传递给 Linq 的 Aggregate() 方法来执行替换。
StringBuilder is used to store the strings generated by its own Replace() 方法:

string theName = "Jason Et Alt., Bruno Mars And More Names";
var matches = Regex.Matches(theName, @"(?!.*?,)\s+?.(\w+)");

string outName = matches.OfType<Match>().Aggregate(new StringBuilder(theName), (sb, m) => 
   sb.Replace(m.Groups[1].Value, new string('X', m.Groups[1].Length))).ToString();

outname = Jason Et Alt., BXXXX MXXX AXX MXXX NXXXX