I would like replace some the chars of the e-mail addresses with * char.
when a customer make request, I would like to hide some the chars of the e-mail addres like below;
ha~~~~@~~~~ail.com
I would like to do that like that. I would like to show first two chars before @ and last 3 chars after @
but is there any other common way of doing this?
Similar to other responses, but also different. Accepts the .co.uk addresses too.
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class Test
{
public static void Main()
{
String regex = @"(.{2}).+@.+(.{2}(?:\..{2,3}){1,2})";
String replace = "$1*@*$2";
List<String> tests = new List<String>(new String[]{
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]"
});
tests.ForEach(email =>
{
Console.WriteLine(Regex.Replace(email, regex, replace));
});
}
}
Results in:
jo*@*le.com
ji*@*ob.com
su*@*co.uk
[email protected]
[email protected]
Though I'm not 100% sure what you want to do with names that only have 2 letters on either side (thus the last two results). But that's my bid. Example
Because your rules are quite simple it might be easier to just use substring to get the characters before and after the @ and then replace them.
Something along the lines of
int index = email.IndexOf('@');
string returnValue = email.Replace(email.Substring(index - 3, 3), "***").Replace(email.Substring(index+1,3), "***");
Although you'll need to first validate that the email address contains enough characters before the @ and change accordingly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With