Java String Camel Case camelCase(String content)

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

Description

Converts `string` to [camel case]

License

Apache License

Parameter

Parameter Description
content a parameter

Declaration

public static String camelCase(String content) 

Method Source Code

//package com.java2s;
/**/*w w  w .  ja v  a  2s .c o  m*/
 * Copyright [2016] Gaurav Gupta
 *
 * 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 {
    public final static String NATURAL_TEXT_SPLITTER = "(\\s+)|(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])";

    /**
     * Converts `string` to [camel case]
     *
     * @param content
     * @return
     * @example
     *
     * 'Foo Bar > 'fooBar', '--foo-bar--' > 'fooBar', '__FOO_BAR__ > 'fooBar'
     */
    public static String camelCase(String content) {
        StringBuilder result = new StringBuilder();
        //        content = content.replaceFirst("[^a-zA-Z0-9]+", EMPTY);//issue job-history => jobhistory
        int i = 0;
        for (String word : content.replaceAll("[^a-zA-Z0-9]", " ").split(NATURAL_TEXT_SPLITTER)) {
            word = word.toLowerCase();
            if (i == 0) {
                result.append(word);
            } else {
                result.append(firstUpper(word));
            }
            i++;
        }
        return result.toString();
    }

    public static String firstUpper(String string) {
        return string.length() > 1 ? string.substring(0, 1).toUpperCase() + string.substring(1)
                : string.toUpperCase();
    }
}

Related

  1. camelCase(final String input)
  2. camelCase(final String prefix, final String str)
  3. camelCase(final String text)
  4. camelCase(final String value, final String delim)
  5. camelCase(String column)
  6. camelCase(String in)
  7. camelCase(String in)
  8. camelCase(String input)
  9. camelCase(String inStr, int start, int end)