Last active
August 29, 2015 14:26
-
-
Save joasgarcia/dcb6080273202427227b to your computer and use it in GitHub Desktop.
Convert string underscore case do camelCase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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