Java Number Format formatPhoneNumber(String number)

Here you can find the source of formatPhoneNumber(String number)

Description

format Phone Number

License

Apache License

Declaration

public static String formatPhoneNumber(String number) 

Method Source Code

//package com.java2s;
/*//from   w  ww  . ja  v a 2  s.c  om
Copyright 2007-2010 WebDriver committers
Copyright 2007-2010 Google Inc.
    
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    
 http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

public class Main {
    public static String formatPhoneNumber(String number) {

        if (isEmpty(number))
            return "";

        String phoneNumber = number.trim();
        if (phoneNumber.length() == 11) {
            StringBuffer buf = new StringBuffer();
            buf.append(phoneNumber.substring(0, 1));
            buf.append("(");
            buf.append(phoneNumber.substring(1, 4));
            buf.append(")");
            buf.append(phoneNumber.substring(4, 7));
            buf.append("-");
            buf.append(phoneNumber.substring(7));
            return buf.toString();
        }

        if (phoneNumber.length() == 10) {
            StringBuffer buf = new StringBuffer();
            buf.append("(");
            buf.append(phoneNumber.substring(0, 3));
            buf.append(")");
            buf.append(phoneNumber.substring(3, 6));
            buf.append("-");
            buf.append(phoneNumber.substring(6));
            return buf.toString();
        }

        if (phoneNumber.length() == 7) {
            StringBuffer buf = new StringBuffer();
            buf.append(phoneNumber.substring(0, 3));
            buf.append("-");
            buf.append(phoneNumber.substring(3));
            return buf.toString();
        }

        return phoneNumber;
    }

    /**
     * Checks if the given string is empty.
     * String is considered as empty if it consists of whitespace characters only also. 
     * @param s string to check
     * @return <code>true</code> if string is empty, otherwise <code>false</code>
     */
    public static boolean isEmpty(String s) {
        return (s == null || s.trim().length() == 0);
    }
}

Related

  1. formatPattern(String number, String pattern)
  2. formatPct(final Number amount)
  3. formatPercentage(Number numerator, Number denominator)
  4. formatPhone(String phoneNumber, String formattingPattern, String countryCode)
  5. formatPhoneNo(String country, String area, String number, String inline)
  6. formatPhoneNumber(String phoneNumber)
  7. formatPrintIntegerToString(long number, boolean printCommasP, boolean printSignP, int radix, int mincol, char padchar, char commachar, int commainterval)
  8. formatRowKey(final long number, final int digits, byte[] dest)
  9. formatSafetyNumber(String digits)