Java examples for java.lang:String Camel Case
convert All Caps To Lower Camel Case, MY_CONSTANT to myConstant
public class Main{ public static void main(String[] argv){ String text = "MY_CONSTANT"; System.out.println(convertAllCapsToLowerCamelCase(text)); }//from w w w. j a v a 2s . c o m /** * Converts a string that contains upper-case letter and underscores (e.g., * constant names) to a camel-case string. For example, MY_CONSTANT is converted * to myConstant. * * @param text the string to convert * * @return a camel-case version of text */ public static String convertAllCapsToLowerCamelCase(String text) { String lowerCase = text.toLowerCase(); while (true) { int i = lowerCase.indexOf('_'); if (i < 0) { break; } String head = lowerCase.substring(0, i); if (i + 1 == lowerCase.length()) { lowerCase = head; break; } else { char charAfterUnderscore = lowerCase.charAt(i + 1); char upperCase = Character.toUpperCase(charAfterUnderscore); if (i + 2 < lowerCase.length()) { String tail = lowerCase.substring(i + 2, lowerCase.length()); lowerCase = head + upperCase + tail; } else { lowerCase = head + upperCase; break; } } } return lowerCase; } }