flutter - flutter how to compare two different format phone numbers
问题描述
I am trying to find if two phone numbers are same or not (Two same phone number may not be in the same format, as +919998245345
is same as 9998245345
and 99982 45345
)
解决方案
For this, you can use contains() dart string method. I have marked the trailing statement as bold, cos, it applies on the String
. Make sure you get the number in String format, or convert it to String and then perform the operation.
Alogrithm
- Convert the number to be compared to String, or get the number as String
- Remove all the white spaces using this code,
your_phone_number_variable.replaceAll(new RegExp(r"\s+"), "")
. So that every number should be having no white spaces in between for smooth operation- Use
contains()
like this, number1.contains(number2)
Code
// this is a comparision between +919998245345 and other numbers
// you can play around and get what you want
void main() {
var _inputPhone = "+919998245345";
var _checkPhone = "9998245345";
var _anotherCheck = "99982 45345";
// checking that white space removal works or not
print(_anotherCheck.replaceAll(new RegExp(r"\s+"), ""));
// I have just removed the spaces from the number which had the white
// space, you can store the value using this code for every data
// for unknown data coming from server side or user side
_anotherCheck = _anotherCheck.replaceAll(new RegExp(r"\s+"), "");
if(_inputPhone.contains(_anotherCheck)){
print('99982 45345 and +919998245345 are same');
}
if(_inputPhone.contains(_checkPhone)){
print('9998245345 and +919998245345 are same');
}
}
Output
9998245345
99982 45345 and +919998245345 are same
9998245345 and +919998245345 are same
推荐阅读
- ios-charts - 如何在 y 轴上显示从“零”开始的 barChart“”条
- objective-c - 如何在 macOS 的非主线程上有一个事件循环?
- numpy - 如何遍历多维数组并将结果保存在数组中?
- python - 尝试在 pycharm 上运行代码以及在 cmd 中键入 pip 时出错
- html - curl 不为某些网站返回任何输出
- c# - TimePicker C# XAML UWP 宽度自定义
- amazon-web-services - AWS Serverless Aurora - 与主节点的通信链路故障。找不到主节点的活动连接
- reactjs - Import' 和'export' 可能只出现在顶层。但它是在顶层和尽头。(盖茨比和反应 JS)
- python - 使用 Python 进行 SQL 注入
- oci8 - 在 Ubuntu 20.4 上安装 PHP 的 OCI8 扩展