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

标签 flutter dart

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

最佳答案

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

算法

  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如何比较两个不同格式的电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63628603/

相关文章:

flutter - 在_WidgetsAppState中找不到路由RouteSettings的生成器(“/”(null)

Flutter 3.7 基于此命令 `flutter create --template=package <package_name>` 创建与之前(3.3)和文档不同的结果

loops - 获取循环中的项目索引以与存储的索引匹配

flutter - 如何将小部件堆叠在TextField下,并使其在加载更多内容时增长到底部?

json - 如何使用动态数组键解析 json?

flutter - 如何在flutter中将文档快照转换为Stream数据?

Flutter UI 组件插件或模块

dart - 如何在页面加载时打开脚手架的抽屉?

Flutter Navigator popUntil方法不会停在我想要的命名路由处,并弹出所有屏幕

json - 从JSON列表中获取数据