Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**
     * Normalizes the phone number format.
     * 
     * @param number the input phone number.
     * @return a normalized phone number.
     */
    public static String normalizePhone(String number) {
        if (number != null && number.length() > 0) {
            if (number.charAt(0) == '+') {
                number = "+" + number.substring(1).replaceAll("[^\\d]", "");

                return number;
            } else {
                number = number.replaceAll("[^\\d]", "");

                if (number.length() == 10)
                    return "+1" + number;
                else if (number.length() == 11 && number.charAt(0) == '1')
                    return "+" + number;
                else if (number.length() > 10)
                    return "+" + number;
                else
                    return "+1" + number;
            }
        } else {
            return "+1";
        }
    }
}