Last active
January 8, 2020 18:11
-
-
Save HudsonAfonso/3aea10cb2714e8d5e466b3d60b4c329b to your computer and use it in GitHub Desktop.
split
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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