stringWithSeparator static method
Separate string with separator you specify
const number = 089111222333;
final result = GlobalFunction.stringWithSeparator('$number', separateEvery: 3);
print(result); // 089-111-222-333
Implementation
static String stringWithSeparator(
String string, {
int separateEvery = 4,
String separator = '-',
}) {
final List<String> tempList = [];
var count = 0;
while (count < string.length) {
int endSubstring = 0;
/// Check if current count + separateEvery offset of total length
/// if offset, take the rest of the available strings
/// otherwise take string with [rumus] separateEvery + count
if (separateEvery + count > string.length) {
// 12 + (13-12)
endSubstring = count + (string.length - count);
} else {
endSubstring = separateEvery + count;
}
final substring = string.substring(count, endSubstring);
tempList.add(substring);
count += separateEvery;
}
final join = tempList.join(separator);
return join;
}