hidePartial 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
String? hidePartial({int begin = 0, int? end, String replace = '*'}) {
final buffer = StringBuffer();
if (length <= 1) {
return null;
}
if (end == null) {
end = (length / 2).round();
} else {
if (end > length) {
end = length;
}
}
for (var i = 0; i < length; i++) {
if (i >= end) {
buffer.write(String.fromCharCode(runes.elementAt(i)));
continue;
}
if (i >= begin) {
buffer.write(replace);
continue;
}
buffer.write(String.fromCharCode(runes.elementAt(i)));
}
return buffer.toString();
}