skip to Main Content

Given an email-string as below

String email = "[email protected]"

I’d like to show in my Flutter code a censored string, e.g:

"na******[email protected]"

I tried email.replaceRange(2, email.length, "*" * (email.length - 2)) with output:

"na******************"

I’d like to stop the censorship before @, maybe 2 or 3 characters before.

2

Answers


  1. String email="[email protected]";
    var nameuser = email.split("@");
    var emailcaracter=email.replaceRange(2,nameuser[0].length,"*" * (nameuser[0].length-2));
    print(emailcaracter);

    result :

    de*******@gmail.com
    Login or Signup to reply.
  2. You can use this method to achieve what you want exactly:

    String unconsore(String string, {int consoreLevel = 2}) {
      final parts = string.split("@");
    
      if (parts.length != 2) {
        // Handle the case where there is no "@" separator in the string
        return string;
      }
    
      final stringBeforeA = parts[0];
      final stringAfterA = parts[1];
    
      final unconsoredBeforeA = stringBeforeA.replaceRange(
          consoreLevel, stringBeforeA.length - consoreLevel, "*" * (stringBeforeA.length - consoreLevel * 2));
    
      return "$unconsoredBeforeA@$stringAfterA";
    }
    
    // example
    print(unconsore("[email protected]")); // na*******[email protected]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search