繁体   English   中英

如何在没有国家代码的情况下联系电话?

[英]How to get phone contact without country code?

我可以通过电话获得联系电话。 我想知道如何从中删除国家/地区代码。 也就是说,如果用户的电话号码带有国家代码(+ 91-9876543210),我需要没有带前缀国家代码的电话号码(9876543210)。

提前致谢。

我建议使用libphonenumber轻松解析电话号码。 您可以通过这种方式拆分国家/地区代码和电话号码。

try {
    // phone must begin with '+'
    PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
    Phonenumber.PhoneNumber numberProto = phoneUtil.parse("+91-9876543210", "");
    int countryCode = numberProto.getCountryCode();
    long nationalNumber = numberProto.getNationalNumber();
    Log.i("code", "code " + countryCode);
    Log.i("code", "national number " + nationalNumber);
} catch (NumberParseException e) {
    System.err.println("NumberParseException was thrown: " + e.toString());
}

使用此作为您的代码......

 import java.util.ArrayList;

public class aaaaa {

    public static void main(String[] args) {
        ArrayList<String> al = new ArrayList<String>();
        al.add("+91");
        String input = "+91-9876543210";
        if (input.indexOf("+") > -1) {
            String str = input.split("-")[0];
            if (al.contains(str)) {
                System.out.println(input.split("-")[1]);
            }
        }
    }
}

将存在/您需要使用的所有国家/地区代码添加到ArrayList al ...或者如果您只需要印度代码,请使用代码...

测试了所有可能的国家/地区代码

如果您不关心国家/地区代码,只是对+xxx-<number>之间的数据感到困扰,那么您可以使用此....

String str="+91-9876543210";;  
System.out.println(str.substring(str.indexOf('-')+1,str.length()));

可以使用以下代码执行此操作。

public static void main(String[] args) {
    String input="+91-9876543210";
    if(input.indexOf("-")>-1)
    System.out.println(input.split("-")[1]);
}

它很容易。

String s="+91-9876543210";;  
System.out.println(s.substring(s.indexOf('-')+1,s.length()));

public static String removeCountryCode(String strMobWithCD){

    String strNewMob = null;

    if(strMobWithCD.contains("+91")){

        strNewMob = strMobWithCD.replace("+91","").toString();
    }else if(strMobWithCD.contains("91")){

        strNewMob = strMobWithCD.replace("91","").toString();
    }else {

        strNewMob = strMobWithCD;
    }

    return strNewMob;
}
import javax.validation.constraints.NotEmpty;

import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber

public class MobileNumberUtil {
    public static String toMobileNoFromInternationPhone(@NotEmpty String 
    internationalMobileNumber) {
        PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
        String nationalNumber = null;
        try {
            Phonenumber.PhoneNumber numberProto = 
            phoneUtil.parse(internationalMobileNumber, "");

            nationalNumber = Long.toString(numberProto.getNationalNumber());
        } catch (NumberParseException e) {
            return StringUtils.EMPTY;
       }
       return nationalNumber;
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM