Moein Hosseini
Moein Hosseini

Reputation: 1596

How to extract double from string in dart?

I have a string containing 3 or 4 double numbers. what's the best way to extract them in an array of numbers?

Upvotes: 3

Views: 1788

Answers (1)

lrn
lrn

Reputation: 71903

First you have to find the numerals. You can use a RegExp pattern for that, say:

var doubleRE = RegExp(r"-?(?:\d*\.)?\d+(?:[eE][+-]?\d+)?");

Then you parse the resulting strings with double.parse. Something like:

var numbers = doubleRE.allMatches(input).map((m) => double.parse(m[0])).toList();

Upvotes: 6

Related Questions