Android Phone Number Get getPlainPhoneNumber(String phoneNumber)

Here you can find the source of getPlainPhoneNumber(String phoneNumber)

Description

Convert a formatted phone number into a plain digit-only phonumber

Parameter

Parameter Description
phoneNumber a parameter

Return

empty string if failed.

Declaration

public static String getPlainPhoneNumber(String phoneNumber) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from  w w w. j  av  a 2  s.  c  o m
     * Convert a formatted phone number into a plain digit-only
     * phonumber
     * @param phoneNumber
     * @return empty string if failed.
     */
    public static String getPlainPhoneNumber(String phoneNumber) {
        if (phoneNumber == null || phoneNumber.isEmpty())
            return "";

        String newPhoneNumber = new String(phoneNumber);

        newPhoneNumber = newPhoneNumber.replace(" ", "");
        newPhoneNumber = newPhoneNumber.replace("(", "");
        newPhoneNumber = newPhoneNumber.replace("-", "");
        newPhoneNumber = newPhoneNumber.replace(")", "");
        newPhoneNumber = newPhoneNumber.replace("+", "");

        if (!newPhoneNumber.startsWith("1")) {
            newPhoneNumber = "1" + newPhoneNumber;
        }

        return newPhoneNumber;
    }
}

Related

  1. getPhoneNumber(Context context)
  2. getMy10DigitPhoneNumber(Context ctx)
  3. getMyPhoneNumber(Context ctx)
  4. getPhoneNumber(int contactId)