Strings { public static String underscore(String word) { String firstPattern = "([A-Z]+)([A-Z][a-z])"; String secondPattern = "([a-z\\d])([A-Z])"; String replacementPattern = "$1_$2"; word = word.replaceAll("\\.", "/"); word = word.replaceAll("\\$", "__"); word = word.replaceAll(firstPattern, replacementPattern); word = word.replaceAll(secondPattern, replacementPattern); word = word.replace('-', '_'); word = word.toLowerCase(); return word; }  static String tableize(String word); static String pluralize(String word); static String singularize(String word); static String underscore(String word); static String camelize(String word); static String camelize(String word, boolean lowercaseFirstLetter); static String depackage(String word); static String foreignKey(String className); static String foreignKey(String className, boolean separateIdWithUnderscore); static String titleize(String word); static String classify(String tableName); static String humanize(String word); static String ordinalize(int n); static String dasherize(String word); static String capitalize(String word);  }
@Test public void testUnderscore() { for (Map.Entry<String, String> entry : camelToUnderscore_.entrySet()) { assertEquals(entry.getValue(), Strings.underscore(entry.getKey())); } for (Map.Entry<String, String> entry : camelToUnderscoreWithoutReverse_.entrySet()) { assertEquals(entry.getValue(), Strings.underscore(entry.getKey())); } }
@Test public void testUnderscoreWithSlashes() { for (Map.Entry<String, String> entry : camelWithPackageToUnderscoreWithSlash_.entrySet()) { assertEquals(entry.getValue(), Strings.underscore(entry.getKey())); } }