Java examples for java.lang:String Camel Case
Write code to Converts a string that contains upper-case letter and underscores (e.g., constant names) to a camel-case string.
//package com.java2s; //from w ww . j a v a 2s . c o m public class Main { public static void main(String[] argv) { String text = "ABC_DEF"; System.out.println(convertAllCapsToLowerCamelCase(text)); } /** * 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; } }