Here you can find the source of camelCase(String content)
Parameter | Description |
---|---|
content | a parameter |
public static String camelCase(String content)
//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(); } }