Java String Camel Case camelCase(String text)

Here you can find the source of camelCase(String text)

Description

Converts the given String to camel case.

License

Open Source License

Declaration


public static String camelCase(String text) 

Method Source Code

//package com.java2s;
// License as published by the Free Software Foundation; either

public class Main {
    /**/* w w  w .  j  av  a2  s. c o  m*/
     * Converts the given String to camel case.
     * <p>
     * The first letter is lowercased, as per Java convention.
     */

    public static String camelCase(String text) {
        return camelCase(text, ' ');
    }

    /**
     * Converts the given String to camel case.
     * <p>
     * The first letter is lowercased, as per Java convention.
     */

    public static String camelCase(String text, char separator) {
        // (use StringBuffer for J2SE 1.4 compatibility)

        StringBuffer buffer = new StringBuffer(text.length());

        // Convert separators to camel case

        boolean lastWasSeparator = false;
        char[] chars = text.toCharArray();

        for (char c : chars) {
            if (c == separator) {
                lastWasSeparator = true;
                continue;
            }

            if (!Character.isLetter(c) && !Character.isDigit(c)) {
                continue;
            }

            if (buffer.length() == 0) {
                buffer.append(Character.toLowerCase(c));
                continue;
            }

            if (lastWasSeparator) {
                buffer.append(Character.toUpperCase(c));
                lastWasSeparator = false;
                continue;
            }

            buffer.append(c);
        }

        return buffer.toString();
    }
}

Related

  1. camelCase(String str)
  2. CamelCase(String str)
  3. camelCase(String string, boolean firstUpper)
  4. camelCase(String text)
  5. camelCase(String text)
  6. camelCase(String text)
  7. camelCased(String str)
  8. camelCasedWord(String s)
  9. camelCaseWord(String word)