【问题标题】:How to do number parsing?如何进行数字解析?
【发布时间】:2014-04-05 15:59:42
【问题描述】:

我正在制作一个使用电话联系人的项目。在这个应用程序中,我将号码(来电号码)与联系人列表中保存的号码匹配。我已经格式化了号码,它可以在以这种格式 +919045308261 保存号码的手机上工作,而不能在以这种格式 +91 90 45 308261 保存号码的手机上工作 我使用的代码是...

int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor
                        .getColumnIndex(HAS_PHONE_NUMBER)));

                if (hasPhoneNumber > 0) {


                    Cursor phoneCursor = contentResolver.query(
                            PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?",
                            new String[] { contact_id }, null);

                    while (phoneCursor.moveToNext()) {


                        phoneNumber = phoneCursor.getString(phoneCursor
                                .getColumnIndex(NUMBER));
                        String formatedphn = PhoneNumberUtils.formatNumber(phoneNumber);
formatedphn = formatedphn.trim();
                        Log.i("formated number: ", formatedphn);
                //here the number is from which the call is incoming        
                        if(number.equals(formatedphn)){
                            Toast.makeText(getApplicationContext(), "got it", Toast.LENGTH_SHORT).show();
                            Log.i("Match occurs", "got a hit");
                            //Log.i("number= ", number);
                            String contname = phoneCursor.getString(phoneCursor
                                    .getColumnIndex(DISPLAY_NAME));
                            if(!contname.equals(null)){
                                Toast.makeText(getApplicationContext(), contname, Toast.LENGTH_SHORT).show();
                                tts.speak(contname+" calling", TextToSpeech.QUEUE_FLUSH, null);

                            }
                        }

【问题讨论】:

    标签: android libphonenumber


    【解决方案1】:

    String.trim() 删除字符串的所有前导和尾随空格,但不删除中间的空格。 要删除字符串的所有空格,您可以使用此正则表达式:

    str = str.replaceAll("\\s+","");
    

    【讨论】:

    • 谢谢它的工作,但你能否再解决我的 1 个问题,在不同的手机中,你知道号码 +919045308261 以不同的方式保存,如 0 90 45 308261、9045308261,90 45 308261。如何我可以将这些与来电的 +919045308261 匹配吗
    • 这个我不知道。考虑针对这个问题提出一个新问题。
    【解决方案2】:

    你可以做下面的事情,去掉字符串中的所有空格后进行比较。

    String st = " Hello world";
    st = st.trim(); // It should work fine and remove all white spaces.
    

    现在在比较时比较两个字符串。 最好的

    【讨论】:

    • 我也做过那件事,但现在也不起作用。我已经在我的代码中进行了更新...
    【解决方案3】:

    以下是一些参考资料:

    【讨论】:

      【解决方案4】:

      根据这个线程How to format a phone number using PhoneNumberUtils? 您可以从 +98 91 28 033921 到这个 0912 80 33921 得到格式化的数字,并与这个进行比较

      【讨论】:

        猜你喜欢
        • 2019-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多