Java String Camel Case Format toCamelCase(String id)

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

Description

Convert an identifier into CamelCase, by capitalizing the first letter and the letter following all underscores, and then removing all underscores.

License

Open Source License

Parameter

Parameter Description
id The id to convert

Return

The CamelCase version of the input parameter.

Declaration

public static String toCamelCase(String id) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from w w w .  j a  v  a2s.  c  o  m
     * Convert an identifier into CamelCase, by capitalizing the first letter
     * and the letter following all underscores, and then removing all
     * underscores. For example, "query_string" becomes "QueryString".
     *
     * @param id
     *            The id to convert
     * @return The CamelCase version of the input parameter.
     */
    public static String toCamelCase(String id) {
        int N = id.length();
        StringBuilder sb = new StringBuilder(2 * N);
        boolean makeUpper = true;
        for (int i = 0; i < N; i++) {
            char ch = id.charAt(i);
            if (ch == '_') {
                makeUpper = true;
            } else {
                if (makeUpper) {
                    ch = Character.toUpperCase(ch);
                    makeUpper = false;
                }
                sb.append(ch);
            }
        }
        return sb.toString();
    }
}

Related

  1. toCamelCase(String anyCase, char wordMarker)
  2. toCamelCase(String argString)
  3. toCamelCase(String begin, String... parts)
  4. toCamelCase(String columnName)
  5. toCamelCase(String dashCase)
  6. toCamelCase(String in, boolean startWithUpper)
  7. toCamelCase(String input)
  8. toCamelCase(String input)
  9. toCamelCase(String input)