Parse phone number

This example will show how to parse a phone number from a formatted string using java, google guava, apache commons and google libphonenumber. A quick little background. The 3-3-4 phone number scheme was first developed by AT&T in 1947. The scheme represents area code, prefix and line number. This example attempts to shows a few flavors on how to parse, split or extract a phone number from a string.

Straight up Java

This snippet will parse a phone number separated with - using String.split. String.split will break the string into three arrays. The first array will represent the area code, second prefix and the third the line number.

@Test
public void parse_phone_number_java () {

    String formattedPhoneNumber = "123-456-7890";

    String [] phoneNumberElements = formattedPhoneNumber.split("-");

    String areaCode = phoneNumberElements[0];
    String prefix = phoneNumberElements[1];
    String lineNumber = phoneNumberElements[2];

    assertEquals("123", areaCode);
    assertEquals("456", prefix);
    assertEquals("7890", lineNumber);

}

Google Guava

This snippet will extract phone number from string using google guava. Using guava Splitter and a traditional formatting convention for phone numbers, we will split the String that contains the phone number on '-', ')' and '('. This will result in three elements within the Iterable representing area code, prefix and line number.

@Test
public void parse_phone_number_guava () {

    String formattedPhoneNumber = "(123) 456-7890";

    Iterable<String> phoneSplit = Splitter.on(CharMatcher.anyOf("-)("))
            .omitEmptyStrings().trimResults().split(formattedPhoneNumber);

    String areaCode = Iterables.get(phoneSplit, 0);
    String prefix = Iterables.get(phoneSplit, 1);
    String lineNumber = Iterables.get(phoneSplit, 2);

    assertEquals("123", areaCode);
    assertEquals("456", prefix);
    assertEquals("7890", lineNumber);
}

Apache Commons

Following a similar approach as the java example above, we will split a phone number from a string using apache commons StringUtils.split.

@Test
public void parse_phone_number_apache_commons () {

    String formattedPhoneNumber = "123-456-7890";

    String [] phoneNumberElements = StringUtils.split(formattedPhoneNumber, "-");

    String areaCode = phoneNumberElements[0];
    String prefix = phoneNumberElements[1];
    String lineNumber = phoneNumberElements[2];

    assertEquals("123", areaCode);
    assertEquals("456", prefix);
    assertEquals("7890", lineNumber);
}

Google phone number library

This snippet will show how to parse a string that contains a phone number using google libphonenumber. PhoneNumberUtil is a utility class for international phone numbers and has a parse method built in where you pass in the string to parse and the default region.

@Test
public void parse_phone_number_google_libphonenumber () throws NumberParseException {

    String formattedPhoneNumber = "(123) 456-7890 x 1234";

    PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
    PhoneNumber phonenumber = phoneUtil.parse(formattedPhoneNumber, "US");

    assertEquals(1, phonenumber.getCountryCode());
    assertEquals("1234", phonenumber.getExtension());
    assertEquals(1234567890, phonenumber.getNationalNumber());
}