Java String Camel Case Format toCamelCase(String start)

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

Description

to Camel Case

License

LGPL

Declaration

public final static String toCamelCase(String start) 

Method Source Code

//package com.java2s;
/**/*from  www. j  a  va  2s.  com*/
 *  Copyright (C) 2008-2014  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.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.gnu.org/licenses/lgpl.html
 *
 *  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 toCamelCase(String start) {
        if (nullOrVoid(start)) {
            return "";
        } else {
            final StringBuffer sb = new StringBuffer();
            for (String s : start.toLowerCase().split("_")) {
                if (sb.length() > 1) {
                    sb.append(Character.toUpperCase(s.charAt(0)));
                } else {
                    sb.append(s.charAt(0));
                }
                sb.append(s.substring(1, s.length()).toLowerCase());
            }
            return sb.toString();
        }
    }

    /**
     * Returns true if the given String is null or void ( "", " ", "  " )
     * @param s
     * @return
     */
    public static boolean nullOrVoid(final String s) {
        if (s == null) {
            return true;
        } else {
            if (s.trim().length() == 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * Split a string using the given char as separator ( simple split without "reg exp" )
     * @param s : the string to split
     * @param c : the separator
     * @return : array of 'tokens' ( never null, size = 0 if the string is null, else 1 to N )
     */
    public static String[] split(String s, char c) {
        if (s != null) {
            char chars[] = s.toCharArray();

            // Count separators
            int count = 0;
            for (int n = 0; n < chars.length; n++) {
                if (chars[n] == c)
                    count++;
            }

            if (count > 0) {
                String[] sTokens = new String[count + 1];
                int iToken = 0;
                int iOffset = 0;
                int iLength = 0;
                for (int i = 0; i < chars.length; i++) {
                    if (chars[i] == c) {
                        //--- Create new token 
                        sTokens[iToken] = new String(chars, iOffset, iLength);
                        iToken++;
                        //--- Reset 
                        iOffset = i + 1;
                        iLength = 0;
                    } else {
                        iLength++;
                    }
                }
                //--- Last Token ( current token ) 
                sTokens[iToken] = new String(chars, iOffset, iLength);
                return sTokens;
            } else {
                //--- No separator
                String[] ret = new String[1];
                ret[0] = s;
                return ret;
            }
        }
        return new String[0];
    }
}

Related

  1. toCamelCase(String s)
  2. toCamelCase(String s)
  3. toCamelCase(String s, boolean firstCharUppercase)
  4. toCamelCase(String s, Boolean firstUpper)
  5. toCamelCase(String s, String separator, boolean capitalizeFirstPart)
  6. toCamelCase(String str)
  7. toCamelCase(String str)
  8. toCamelCase(String str)
  9. toCamelCase(String str)