Skip to content

Instantly share code, notes, and snippets.

@HudsonAfonso
Last active January 8, 2020 18:11
Show Gist options
  • Save HudsonAfonso/3aea10cb2714e8d5e466b3d60b4c329b to your computer and use it in GitHub Desktop.
Save HudsonAfonso/3aea10cb2714e8d5e466b3d60b4c329b to your computer and use it in GitHub Desktop.
split
void main() {
var numCartao = splitIntoChunks(string: '3991641839424977', size: 4);
print(numCartao);
var numCartao2 = '3991641839424977'.splitIntoChunks(size: 4);
print(numCartao2);
}
String splitIntoChunks({String string, int size, String separator = ' '}) {
return string.splitMapJoin(
RegExp(".{1,$size}"),
onMatch: (Match m) => '${m.group(0)}$separator',
onNonMatch: (String n) => n,
);
}
extension SplitIntoChunks on String {
String splitIntoChunks({int size, String separator = ' '}) {
return this.splitMapJoin(
RegExp(".{1,$size}"),
onMatch: (Match m) => '${m.group(0)}$separator',
onNonMatch: (String n) => n,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment