I would like to retrieve only the number values from a string on flutter without keeping the text hardcoded using replaceAll, the text can be anything but the number part of it has to be retrieved from it.
e.g.
String text = "Hello your number is: 1234";
String numberProvided = '1234'; // needs to be extracted from String text
print("The number provided is :" numberProvided);
Like I said, the text characters shouldn't be hardcoded into the application, let me know if it is possible, thanks!
CodePudding user response:
Use the simple regular expression
print(text.replaceAll(RegExp("[a-zA-Z:\s]"), ""));
CodePudding user response:
Try below code hope its help to you. refer replaceAll
method here
void main() {
String text = "Hello your number is: 1234567890";
var aString = text.replaceAll(RegExp(r'[^0-9]'), '');
var aInteger = int.parse(aString);
print(
"The number provided is :" aInteger.toString(),
);
}
Your Output:
The number provided is :1234567890