How to get phone contact without country code?
Use this as your code...
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]);
}
}
}
}
Add all the country codes that exist/you need to use into the ArrayList al
... Or use the code as it is if you just need Indian code...
Tested with all possible country codes.
If you don't care about the country codes and just bothered about the data between +xxx-<number>
, then you can use this....
String str="+91-9876543210";;
System.out.println(str.substring(str.indexOf('-')+1,str.length()));
I'd suggest to use libphonenumber to parse the phone numbers easily. You can split the country code and phone number in this way.
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());
}