Dart/Flutter: Split string every nth character?
3,120
For any size split and making a list.
void main() {
final splitSize = 2;
RegExp exp = new RegExp(r"\d{"+"$splitSize"+"}");
String str = "0102031522";
Iterable<Match> matches = exp.allMatches(str);
var list = matches.map((m) => int.tryParse(m.group(0)));
print(list);
}
Tested on dartpad
Author by
Michael
Updated on December 20, 2022Comments
-
Michael over 1 year
I have a single string made up of two digit numbers with leading zeros (ie '0102031522')
that I want to split into a list as integers without the leading zeros.
Output of this example should be
[1,2,3,15,22]
.I'm having trouble trying to get this converted as Dart is new to me, and i have no clue where to start. Any suggestions?
-
Michael almost 4 yearsThank you @Doc. I'm trying to make this work, but it's just producing the numbers, not in a list. How do I put them in a list of integers like
[1,2,3,15,22]
? -
Michael almost 4 yearsPerfect. Thank you!