Hammadh
Hammadh

Reputation: 13

How to remove text character values from string on flutter

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!

Upvotes: 1

Views: 4364

Answers (2)

Ravindra S. Patil
Ravindra S. Patil

Reputation: 14775

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

Upvotes: 1

Kuntec
Kuntec

Reputation: 92

Use the simple regular expression

    print(text.replaceAll(RegExp("[a-zA-Z:\s]"), ""));

Upvotes: 6

Related Questions