get Camel Case String - Java java.lang

Java examples for java.lang:String Camel Case

Description

get Camel Case String

Demo Code

/*******************************************************************************************
 *   Copyright (c) 2016, zzg.zhou(11039850@qq.com)
 * //from   w w w .java  2s  .c  o m
 *  Monalisa is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.

 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.

 *   You should have received a copy of the GNU Lesser General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************************/
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String inputString = "java2s.com";
        boolean firstCharacterUppercase = true;
        System.out.println(getCamelCaseString(inputString,
                firstCharacterUppercase));
    }

    public static String getCamelCaseString(String inputString,
            boolean firstCharacterUppercase) {
        StringBuilder sb = new StringBuilder();

        boolean nextUpperCase = false;
        for (int i = 0; i < inputString.length(); i++) {
            char c = inputString.charAt(i);
            if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')
                    || (c >= 'A' && c <= 'Z')) {
                if (c >= '0' && c <= '9' && sb.length() == 0) {
                    sb.append("N");
                }

                if (nextUpperCase) {
                    sb.append(Character.toUpperCase(c));
                    nextUpperCase = false;
                } else {
                    sb.append(Character.toLowerCase(c));
                }
            } else if ((c >= 0x4e00) && (c <= 0x9fbb)) {
                //?
                sb.append(c);
            } else {
                if (sb.length() > 0) {
                    nextUpperCase = true;
                }
            }
        }

        if (firstCharacterUppercase) {
            sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
        }

        return sb.toString();
    }
}

Related Tutorials