我正在尝试查找两个电话号码是否相同(两个相同的电话号码可能格式不同,因为 +919998245345
与 9998245345
相同和99982 45345
)
最佳答案
为此,您可以使用 contains() Dart 串法。我已将尾随语句标记为粗体,因为它适用于String
。确保您获取的是字符串格式的数字,或者将其转换为字符串然后执行操作。
算法
- 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)
代码
// 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如何比较两个不同格式的电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63628603/