Here you can find the source of formatPhoneNumber(String number)
public static String formatPhoneNumber(String number)
//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); } }