I have a List that contains lottery numbers List image

How can I separate number and text, and after that list them in the below list: for example:

  List<String> n = ["ABC23", "21A23", "12A312","32141A"];

Thank you!


Solution 1: Abbas Jafari

You can do it by using forEach like below:

List<String> n = ["23", "2123", "12312","32141"];
n.forEach((element) => 
    print(element)
);

And to separate number and text you can use the following code:

const text = '''
Lorem Ipsum is simply dummy text of the 123.456 printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an 12:30 unknown printer took a galley of type and scrambled it to make a
23.4567
type specimen book. It has 445566 survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
''';
final intRegex = RegExp(r'\s+(\d+)\s+', multiLine: true);
final doubleRegex = RegExp(r'\s+(\d+\.\d+)\s+', multiLine: true);
final timeRegex = RegExp(r'\s+(\d{1,2}:\d{2})\s+', multiLine: true);
void main() {
  print(intRegex.allMatches(text).map((m) => m.group(0)));
  print(doubleRegex.allMatches(text).map((m) => m.group(0)));
  print(timeRegex.allMatches(text).map((m) => m.group(0)));
}

Please refer to this link for more information.


Solution 2: Robert Sandberg

To do something with each element:

List<String> n = ["23", "2123", "12312","32141"];
n.forEach((element) {
    int number = int.parse(element);
    ...
  }
);

To do create list of ints:

List<String> n = ["23", "2123", "12312","32141"];
final intList = n.map((element) => int.parse(element)).toList();