flutter 在Dart中隐藏电子邮件地址的中心字符

lzfw57am  于 2023-02-25  发布在  Flutter
关注(0)|答案(2)|浏览(189)

给定如下的电子邮件字符串

String email = "namesurname@mail.com"

我想在我的Flutter代码中显示一个经过审查的字符串,例如:

"na******ame@mail.com"

我尝试了email.replaceRange(2, email.length, "*" * (email.length - 2)),输出为:

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

我想在@之前停止审查,也许在2或3个字符之前。

kr98yfug

kr98yfug1#

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

结果:

de*******@gmail.com
bq9c1y66

bq9c1y662#

您可以使用此方法来实现您确切想要的结果:

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("namesurname@mail.com")); // na*******me@mail.com

相关问题