1

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!

3
  • you want to print only numbers in text String? Commented Dec 7, 2021 at 12:06
  • @RavindraS.Patil yes i want to retrieve only the number values from a string that includes both text and numbers Commented Dec 7, 2021 at 12:15
  • Check my answer hope its help to you. Commented Dec 7, 2021 at 12:18

2 Answers 2

6

Use the simple regular expression

    print(text.replaceAll(RegExp("[a-zA-Z:\s]"), ""));
Sign up to request clarification or add additional context in comments.

1 Comment

Add trim at the end to avoid unnecessary spaces lile this : print( text.replaceAll(RegExp("[a-zA-Z:\s]"), "").trim());
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.