gpt4 book ai didi

flutter - flutter 如何比较两个不同格式的电话号码

转载 作者:行者123 更新时间:2023-12-03 03:14:09 24 4
gpt4 key购买 nike

我正在尝试查找两个电话号码是否相同(两个相同的电话号码可能格式不同,因为+919998245345999824534599982 45345相同)

最佳答案

为此,可以使用contains() Dart 字符串方法。我将尾随语句标记为粗体,因为它适用于String。确保以字符串格式获取数字,或将其转换为字符串,然后执行操作。
Alogrithm

  1. Convert the number to be compared to String, or get the number as String
  2. 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
  3. Use contains() like this, number1.contains(number2)

代码
// 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');
}

}
输出
9998245345
99982 45345 and +919998245345 are same
9998245345 and +919998245345 are same

关于flutter - flutter 如何比较两个不同格式的电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63628603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com