Here you can find the source of formatPhone(String phone)
public static String formatPhone(String phone)
//package com.java2s; public class Main { public static String formatPhone(String phone) { return formatPhone(phone, null); }// w w w .j a v a 2 s . c o m public static String formatPhone(String phone, String format) { if (isNullOrEmpty(phone)) { return phone; } String s = ""; char[] cs = getDigit(phone).toCharArray(); int i = 0; if (isNullOrEmpty(format)) { if (cs[0] == '1') { format = "1 (ddd) ddd-dddd"; i++; } else { format = "(ddd) ddd-dddd"; } } for (char c : format.toCharArray()) { if (c == 'd') { s += cs[i++]; if (i > cs.length - 1) { break; } } else { s += c; } } return s; } public static boolean isNullOrEmpty(String input) { if (input == null) { return true; } return input.trim().isEmpty(); } public static String getDigit(String input) { if (isNullOrEmpty(input)) { return input; } String s = ""; for (char c : input.toCharArray()) { if (c >= '0' && c <= '9') { s += c; } } return s; } }