[英]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.