首页 > 解决方案 > 如何从 Dart/Flutter 中的字符串(例如电话号码)中提取数字?

问题描述

我有几种类型的电话号码作为字符串:

例如:

+49 176 666 454 414

+49(176)-666454414

+49176-666454414

但我想转换每种类型的电话号码,如下所示: +4917622265414

基本上只有数字。

我怎么能在飞镖中做到这一点?Aynone有想法吗?我可以像下面那样做,但我相信一定有更好的方法来做到这一点。

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

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

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

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

谢谢 :)

标签: stringflutterdart

解决方案


您可以使用正则表达式从字符串中提取所有数字

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

并在开头添加+

result = "+$result";

它对你有帮助吗?


推荐阅读