Java String Camel Case Format toCamelCase(String text)

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

Description

Takes a string, removes all characters that are not letters or digits and capitalizes the next letter following a series of characters that are not letters or digits.

License

Apache License

Parameter

Parameter Description
text The text to camel case

Return

The camel cased version of the string given

Declaration

public static String toCamelCase(String text) 

Method Source Code

//package com.java2s;
/**//ww w  .j ava  2s  . c o m
 * Copyright (C) 2014-2015 LinkedIn Corp. (pinot-core@linkedin.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Takes a string, removes all characters that are not letters or digits and capitalizes the next letter following a
     * series of characters that are not letters or digits. For example, toCamelCase("Hello world!") returns "HelloWorld".
     *
     * @param text The text to camel case
     * @return The camel cased version of the string given
     */
    public static String toCamelCase(String text) {
        int length = text.length();
        StringBuilder builder = new StringBuilder(length);

        boolean capitalizeNextChar = false;

        for (int i = 0; i < length; i++) {
            char theChar = text.charAt(i);
            if (Character.isLetterOrDigit(theChar) || theChar == '.') {
                if (capitalizeNextChar) {
                    builder.append(Character.toUpperCase(theChar));
                    capitalizeNextChar = false;
                } else {
                    builder.append(theChar);
                }
            } else {
                capitalizeNextChar = true;
            }
        }

        return builder.toString();
    }
}

Related

  1. toCamelCase(String string)
  2. toCamelCase(String string)
  3. toCamelCase(String stringValue, String delimiter)
  4. toCamelCase(String text)
  5. toCamelCase(String text)
  6. toCamelCase(String text, boolean capFirstLetter)
  7. toCamelCase(String value)
  8. toCamelCase(String value)
  9. toCamelCase(String value)