Ayrix
Ayrix

Reputation: 491

How to extract digits from String (e.g. a phone number) in Dart/Flutter?

I've got several types of phone numbers as a String:

For example:

+49 176 666 454 414

+49(176)-666454414

+49176-666454414

But I would like to convert every type of phone number like the following : +4917622265414

Basically only digits.

How can I do that in dart? Aynone has an idea? I could do like below but I'm sure there must be a better way to do that.

String result = gfg.replaceAll(" ", "");

String result = gfg.replaceAll("(", "");

String result = gfg.replaceAll(")", "");

String result = gfg.replaceAll("-", "");

Thanks :)

Upvotes: 1

Views: 2046

Answers (1)

Abdel Matyne
Abdel Matyne

Reputation: 93

You can use regex to extract all digit from the string

String result = gfg.replaceAll(new RegExp(r'[^0-9]'),'');

And add the + at the begining

result = "+$result";

Does it help you ?

Upvotes: 4

Related Questions