Let's say I have a string that consists of a person's name:
var name = "Jason, Bruno Mars";
How do I mask the string
with X
for the name behind the comma and returns:
var name = "Jason, BXXXX MXXX";
I have tried using the following methods but only front characters are masked:
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;
}
}
Anyone has a better idea of how can I achieve the same result in a more efficient way?
Another option, using Regex.Matches to select the name parts except the first letter. The regex collects all string parts separated by a space, skipping what's before the comma.
The collections of Matches
is then passed to Linq's Aggregate() method to perform the substitution.
A StringBuilder is used to store the strings generated by its own Replace() method:
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
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;
}
Try This
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;
}
Efficient way of masking without Split
, Regex
, and using System.Linq
:
For C# version < 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;
}
}
For C# version >= 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();
}
}
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.