I am trying to mask some confidential information which can be any alphanumeric string for ex .
Example 1
Before masking - PMGKJGFWB125 After masking - PMG******125
Example 2
Before masking - 19000 After masking - 1**00
I was trying something like this in C#
Regex.Replace(s, @"\d(?!\d{0,3}$)", "*")
The length of string may vary so we cannot add hardcoded offsets .
Can I get some help on this ?
Thanks in advance
Looks like, roughly roughly, you want to divide your string up into quarters and then star out the middle two quarters
Don't get too technical..
var str = "PMGKJGFWB125".ToCharArray();
for(int x = str.Length/4; x < str.Length*3/4; x++)
str[x] = '*';
return new string(str);
You can adjust the 4 for different length strings e.g. if your string is 6 or less maybe do 3 instead of 4.. etc
If you want it as LINQ, for a "WTH is that?!" laugh:
(int f, int t) = (str.Length/4, str.Length*3/4);
return string.Concat(str.Select((c,i)=>f<i&&i<t?'*':c)));