Advertisements
I’m trying to achieve this J*** D**
from John Doe
string but my current code output is **** ***
.
Here is my current code:
void main() {
String txt = 'John Doe';
String hideStr = txt.replaceAll(RegExp(r'\S'), '*');
print(hideStr);
}
any suggestion?
>Solution :
You can use negative look behind to exclude the character at start and after white-space
void main() {
String txt = 'John Doe';
String hideStr = txt.replaceAll(RegExp(r'(?<!^|\s)[^\s]'), '*');
print(hideStr);
}
Because "J" and "D" sit after start of text and a space, the regex won’t match it