September 18, 2012

Google Guava Goodness: Converting Between ASCII Case Conventions

The Google Guava libraries contains useful utility classes and methods. If we want to convert between ASCII case conventions we can use the CaseFormat class. The class defines constants for upper and lower case CamelCase, upper and lower case hyphenated and upper case underscore. This means we can convert UPPER_VALUE to upper-value with a simple line of code.

import static*;

assert, "lowerCase").equals("LOWER_CASE");
assert, "manual-index").equals("manual_index");
assert, "UpperCase").equals("upper_case")

(Sample with Google Guava version 13.0.1)