Java String Camel Case Format toCamelCase(String input, boolean capitalizeFirsLetter)

Here you can find the source of toCamelCase(String input, boolean capitalizeFirsLetter)

Description

Converts a string to camel case, so "Get \"some\" 'big' object" becomes "getSomeObject".

License

Open Source License

Parameter

Parameter Description
input The string to convert
capitalizeFirsLetter Self-explanatory

Declaration

public static String toCamelCase(String input, boolean capitalizeFirsLetter) 

Method Source Code

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

public class Main {
    /**/*from   www .  java 2  s . c  o  m*/
     * Converts a string to camel case, so "Get \"some\" 'big' object" becomes "getSomeObject".
     * @param input The string to convert
     * @param capitalizeFirsLetter Self-explanatory
     */
    public static String toCamelCase(String input, boolean capitalizeFirsLetter) {
        if (input == null)
            return null;
        input = input.replaceAll("[^A-Za-z0-9 ]", "").trim();
        StringBuilder result = new StringBuilder(input.length());
        char[] chars = input.toCharArray();
        for (int n = 0; n < chars.length; n++) {
            if (n == 0) {
                result.append(
                        capitalizeFirsLetter ? Character.toUpperCase(chars[n]) : Character.toLowerCase(chars[n]));
            } else {
                if (chars[n] != ' ')
                    result.append(chars[n]);
                else {
                    while (chars[n] == ' ')
                        n++;
                    result.append(Character.toUpperCase(chars[n]));
                }
            }
        }
        return result.toString();
    }
}

Related

  1. toCamelCase(String id)
  2. toCamelCase(String in, boolean startWithUpper)
  3. toCamelCase(String input)
  4. toCamelCase(String input)
  5. toCamelCase(String input)
  6. toCamelCase(String input, boolean firstCharUppercase, char separator)
  7. toCamelCase(String inputString)
  8. toCamelCase(String inputString)
  9. toCamelCase(String name)