Convert String into CAMEL CASE - Android (JAVA)

 To convert any String into Camel Case with java you can use the following function, it will return you your given string into Camel Case format.


    public static String toCamelCase(String string) {

if (string == null) {
return null;
}
boolean whiteSpace = true;
StringBuilder builder = new StringBuilder(string);
final int builderLength = builder.length();
for (int i = 0; i < builderLength; ++i) {
char c = builder.charAt(i);
if (whiteSpace) {
if (!Character.isWhitespace(c)) {
builder.setCharAt(i, Character.toTitleCase(c));
whiteSpace = false;
}
} else if (Character.isWhitespace(c)) {
whiteSpace = true;
} else {
builder.setCharAt(i, Character.toLowerCase(c));
}
}
return builder.toString();
    }

You can use the give function like this:


    String value = "hello world";
    Log.v("String","Converted String: "+toCamelCase(value));

Hope this will e helpful:)

Comments