在电话号码格式有问题的情况下,匹配电话号码的最佳方法应该是什么?
我正在寻找一种标准丰富的最佳实践方法来做到这一点。我让用户输入一个电话号码并保存到sqlite数据库。后来一个时间点,当一个电话打来时,我通过iTelephony得到了这个号码。我必须将db中的值与incomingnumber匹配并采取行动。这些数字的格式可能大不相同,并可能导致问题。
为了解决这个问题,我现在使用regex来消除格式化字符,只留下数字来获得正确的匹配。这很好,但我想知道是否有人有更好的方法来做这件事,或者android是否有一个api方法来匹配两个电话号码。
谢谢你的关心
编辑:我使用的代码是:

    private boolean matchPhoneNumbers(String numberA, String numberB)
    {
        //Remove everything that's not a number
        numberA=numberA.replaceAll("[^0-9]", "");
        numberB=numberB.replaceAll("[^0-9]", "");
        //Get the minimum length of one of the strings
        int length=Math.min(numberA.length(),numberB.length());
        numberA=numberA.substring((numberA.length()-length));
        numberB=numberB.substring((numberB.length()-length));
        //check the difference
        return numberA.equals(numberB);
    }

最佳答案

检查每个字符,去掉任何小于0或大于9的字符。(简单的while循环)检查右边的10个字符是否相同。

10-01 18:37