TransWikia.com

EMP id Masking with asterisk inbetween string

Stack Overflow Asked by Bala on December 30, 2021

I require a output string like this Th****23 and my input will be Th423423. I have tried below code but not working C# Visual studio.

Input: Th423423 getting output: T*h***423423

mskdStrngBufOb = new StringBuilder(stringToBeMasked);
int lengthOfString = stringToBeMasked.Length;
int maxMaskableLength = (lengthOfString / 2);

if (((lengthOfString % 2) != 0))
{
    maxMaskableLength++;
}

int maskCurPosX = (maxMaskableLength - 1);
int markCurPosY = (maskCurPosX + 1);
int currTotMskdLnth = 0;

while ((currTotMskdLnth < maxMaskableLength))
{

    if ((maskCurPosX > 0))
    {
        mskdStrngBufOb.Insert(maskCurPosX, '*');
        maskCurPosX = (maskCurPosX - 1);
        currTotMskdLnth++;
    }
}

2 Answers

One of the problems here is that you're using the Insert method to add the mask characters to the string, and that method just inserts the character - it doesn't replace it.

Instead, it might be a better idea to simply walk through the string and append characters to the string builder, using the mask character where needed.

However in this case we're really dealing with only three strings:

  1. The first part of the input string
  2. The masked part of the string
  3. The last part of the input string

So, if we can determine where the mask should start and how long it should be, we can just use the Substring method to grab the first and last part of the input string, and add a string of mask characters between them:

public static string ApplyMask(string input, char maskChar = '*')
{
    // If the string is 2 characters or less, just return it
    if (string.IsNullOrEmpty(input) || input.Length < 3) return input;

    // Get the length and start position of mask
    var maskLength = input.Length / 2;
    var maskStart = (input.Length - maskLength) / 2;

    // Adjust mask length so there's an even number of characters 
    // showing before and after it (change -- to ++ for more mask)
    if (input.Length % 2 != maskLength % 2) maskLength--;

    return input.Substring(0, maskStart) +           // First part
           new string(maskChar, maskLength) +        // Mask part
           input.Substring(maskStart + maskLength);  // Last part
}

Here's a little program to test it:

public static void Main(string[] args)
{
    while (true)
    {
        Console.Write("Enter a string: ");
        var input = Console.ReadLine();
        Console.WriteLine($"Masked result:  {ApplyMask(input)}");
        Console.WriteLine(new string('-', 10));
    }
}

Output

enter image description here

Answered by Rufus L on December 30, 2021

If I understand correctly you want to obfuscate a username or some string value.

You could use a regular expression and just take the first and last 2 characters from the string.

Then print whatever in between.

string thevalue = "ThisValue1234";

if (thevalue.Length < 5)
    Console.WriteLine("not long enough");
else
{
    Regex regPat = new Regex("(^.{2}).+(.{2}$)");
    var match = regPat.Match(thevalue);
    if (match.Groups.Count > 0)
    {
        var result = $"{match.Groups[1].Value}***{match.Groups[2].Value}";
        Console.WriteLine(result);
    }
}

If you need to keep the sting the same length then try something like this in the if statement.

//same length
var starArray = Enumerable.Repeat("*", thevalue.Length - 4).ToArray();
var starString = string.Concat(starArray);

StringBuilder sb = new StringBuilder();
sb.Append(match.Groups[1]);
sb.Append(starString);
sb.Append(match.Groups[2]);

result = sb.ToString();

//or you could use
// result = $"{match.Groups[1]}{starString}{match.Groups[2]}";

Console.WriteLine(result);

Answered by CobyC on December 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP