Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract number only from string in flutter?

Tags:

flutter

dart

Lets say that I have a string:

a="23questions";
b="2questions3";

Now I need to parse 23 from both string. How do I extract that number or any number from a string value?

like image 356
Zero Live Avatar asked Apr 24 '20 05:04

Zero Live


2 Answers

The following code can extract the number:

aStr = a.replaceAll(new RegExp(r'[^0-9]'),''); // '23'

You can parse it into integer using:

aInt = int.parse(aStr);
like image 144
Hashir Baig Avatar answered Sep 23 '22 22:09

Hashir Baig


const text = "23questions";

Step 1: Find matches using regex:

final intInStr = RegExp(r'\d+');

Step 2: Do whatever you want with the result:

void main() {
  print(intInStr.allMatches(text).map((m) => m.group(0)));
}
like image 30
Yudhishthir Singh Avatar answered Sep 26 '22 22:09

Yudhishthir Singh