How to Get Numbers from String in Flutter/Dart

In this example, you will learn how to extract or get numbers only from any kind of string in Dart or Flutter. We will use the Regex to extract numbers from string, see the example below:

String str = "Hafce00453dfg234fdksd";
String newnum = str.replaceAll(RegExp(r'[^0-9]'),'');
print(newnum); //output: 00453234

Here, you can get the numbers on string format, where zero "0" is also attached on first, you can later on convert to integer to remove the 0 from front and get the actual number on int format. 

String str = "Hafce00453dfg234fdksd";

//get the integer
int newnum1 = int.parse(str.replaceAll(RegExp(r'[^0-9]'),''));
print(newnum1); //output: 453234

//int newnum2 = int.parse(str);
//dont do this: Error: Invalid radix-10 number

Here, you will get the actual integer without 0 at front of number. 

In this way, you can get Number or Integer only from String using Dart in Flutter. 

No any Comments on this Article

Please Wait...