string - 如何从 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("-", "");
谢谢 :)
解决方案
您可以使用正则表达式从字符串中提取所有数字
String result = gfg.replaceAll(new RegExp(r'[^0-9]'),'');
并在开头添加+
result = "+$result";
它对你有帮助吗?
推荐阅读
- javascript - 有没有办法通过 Python 加载网页的网络活动(您可以在 Chrome 开发工具上看到)?
- postgresql - 在 Postgres 中有效存储大量数据(数百万行)的一些策略是什么?
- c++ - 在SFML中更改坐标系中的参考点后,坐标计算不正确
- ansible - Ansible 存档删除选项不起作用
- symfony - PhpUnit 未通过 Docker 中的 PhpStorm 远程解释器执行
- vue.js - 在 Vue-CLI 的公共文件夹中导入 JSON 文件的方法
- javascript - HTML5 视频不能从 javascript 自动播放
- amazon-web-services - Windows Server 2016 ADFS - 与 AWS Directory Service 集成
- c - 如何修复我的 cs50 pset 1 mario 代码不太舒服
- java - JavaFX中场景背景与新标签之间的问题