hidePartial static method
Replaces chars of the given String s
with replace
.
The default value of replace
is *.
begin
determines the start of the 'replacing'. If begin
is null, it starts from index 0.
end
defines the end of the 'replacing'. If end
is null, it ends at s
length divided by 2.
If s
is empty or consists of only 1 char, the method returns null.
Example : 1234567890 => *****67890 1234567890 with begin 2 and end 6 => 127890 1234567890 with begin 1 => 167890
Implementation
static String? hidePartial(String s,
{int begin = 0, int? end, String replace = '*'}) {
var buffer = StringBuffer();
if (s.length <= 1) {
return null;
}
if (end == null) {
end = (s.length / 2).round();
} else {
if (end > s.length) {
end = s.length;
}
}
for (var i = 0; i < s.length; i++) {
if (i >= end) {
buffer.write(String.fromCharCode(s.runes.elementAt(i)));
continue;
}
if (i >= begin) {
buffer.write(replace);
continue;
}
buffer.write(String.fromCharCode(s.runes.elementAt(i)));
}
return buffer.toString();
}