Skip to content

Instantly share code, notes, and snippets.

@joasgarcia
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joasgarcia/dcb6080273202427227b to your computer and use it in GitHub Desktop.
Save joasgarcia/dcb6080273202427227b to your computer and use it in GitHub Desktop.
Convert string underscore case do camelCase
//Code forked from John Moore (http://grails.1312388.n4.nabble.com/template/NamlServlet.jtp?macro=user_nodes&user=111982)
public static String propertyNameCase(String s) {
// Handle null and empty strings.
if(s == null || s.trim().length() == 0 ) return s;
String str;
if (s.indexOf('_') != -1) {
StringBuilder buf = new StringBuilder();
String[] tokens = s.split("_");
for (String token: tokens) {
if (token == null || token.length() == 0) continue;
buf.append(token.substring(0, 1).toUpperCase()).append(token.substring(1).toLowerCase());
}
str = buf.toString();
} else {
str = s;
}
if (str.length()> 0) {
str = str.substring(0, 1).toLowerCase() + str.substring(1);
}
return str;
}
assert underscoreToCamelCase("TEST_METHOD") = "testMethod"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment