Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace part of string with asterisk in Flutter?

Tags:

flutter

dart

I want to replace part of the string with asterisk (* sign). How can I achieve that? Been searching around but I can't find a solution for it.

For example, I getting 0123456789 from backend, but I want to display it as ******6789 only.

Please advise. Many thanks.

like image 383
user3180690 Avatar asked Dec 22 '22 19:12

user3180690


1 Answers

Try this:

void main(List<String> arguments) {
  String test = "0123456789";
  int numSpace = 6;
  String result = test.replaceRange(0, numSpace, '*' * numSpace);
  print("original: ${test}  replaced: ${result}");
}

Notice in dart the multiply operator can be used against string, which basically just creates N version of the string. So in the example, we are padding the string 6 times with'*'.

Output:

original: 0123456789  replaced: ******6789
like image 134
OldProgrammer Avatar answered Jan 12 '23 01:01

OldProgrammer