Formats a number stripping it from any non digit except for a leading plus sign - Android java.lang

Android examples for java.lang:String Strip

Description

Formats a number stripping it from any non digit except for a leading plus sign

Demo Code

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

public class Main{

    /**//ww  w.ja  va2s  .  c o  m
     * Formats a number stripping it from any non digit except for a leading plus sign
     * @param number
     * @return
     */
    public static String formatPhoneNumber(String number) {
        if (number != null) {
            String strippedNumber = number.replaceAll("\\D", "");
            if (number.startsWith("+")) {
                return "+" + strippedNumber;
            } else {
                return strippedNumber;
            }
        }
        return null;
    }

}

Related Tutorials