fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. public class Test
  7. {
  8. public static void Main()
  9. {
  10. string email = "fajarsyairillah@mail.com";
  11. string Email = maskEmail(email);
  12. string Email2 = maskEmailNoregex(email);
  13. Console.WriteLine("Email :{0}\nEmail2: {1}", Email, Email2);
  14. }
  15. public static string maskEmail(string email)
  16. {
  17. if (string.IsNullOrEmpty(email)) return email;
  18. return Regex.Replace(email, @"(?<=^[^@]{2,})[^@](?=[^@]{2,}@)", "*");
  19. }
  20. public static string maskEmailNoregex(string email)
  21. {
  22. if (string.IsNullOrEmpty(email)) return email;
  23. var split = email.Split('@');
  24. if (split.GetLength(0) != 2 || split[0].Length < 5) return email; // There is nothing to modify
  25. return split[0].Substring(0, 2) + new string('*', split[0].Length-4) + split[0].Substring(split[0].Length - 2) + "@" + split[1];
  26. }
  27. }
  28.  
Success #stdin #stdout 0.07s 20976KB
stdin
Standard input is empty
stdout
Email :fa***********ah@mail.com
Email2: fa***********ah@mail.com