Java String Camel Case Format toCamelCase(String s)

Here you can find the source of toCamelCase(String s)

Description

Force the first character to be lower case.

License

Open Source License

Declaration

public static String toCamelCase(String s) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/* www . j  a  va  2  s  .co m*/
     * Force the first character to be lower case.  If there are spaces in
     * the string, remove them and replace the next character with a
     * capitalized version of itself.
     */
    public static String toCamelCase(String s) {
        String str = s.trim();
        StringBuilder buffer = new StringBuilder();
        boolean capitalizeNext = true; // signal to capitalize next char

        for (int i = 0; i < str.length(); i++) {
            // If there is a none-alphanumeric character, skip it and insert
            // the next character capitalized
            if (!Character.isLetterOrDigit(str.charAt(i))) {
                capitalizeNext = true;
                continue;
            }

            if (capitalizeNext) { // will capitalize the first character
                buffer.append(Character.toUpperCase(str.charAt(i)));
                capitalizeNext = false;
                continue;
            }

            // Otherwise, just copy
            buffer.append(str.charAt(i));
        }

        return buffer.toString();
    }
}

Related

  1. toCamelCase(String s)
  2. toCamelCase(String s)
  3. toCamelCase(String s)
  4. toCamelCase(String s)
  5. toCamelCase(String s)
  6. toCamelCase(String s)
  7. toCamelCase(String s)
  8. toCamelCase(String s)
  9. toCamelCase(String s)