Java String Camel Case Format toCamelCase(String str)

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

Description

to Camel Case

License

Open Source License

Declaration

public static String toCamelCase(String str) 

Method Source Code

//package com.java2s;
/*//from  w  ww  .j av  a 2  s.c o  m
 * Copyright: (c) 2004-2010 Mayo Foundation for Medical Education and 
 * Research (MFMER). All rights reserved. MAYO, MAYO CLINIC, and the
 * triple-shield Mayo logo are trademarks and service marks of MFMER.
 *
 * Except as contained in the copyright notice above, or as used to identify 
 * MFMER as the author of this software, the trade names, trademarks, service
 * marks, or product names of the copyright holder shall not be used in
 * advertising, promotion or otherwise in connection with this software without
 * prior written authorization of the copyright holder.
 * 
 * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/legal/epl-v10.html
 * 
 */

public class Main {
    public static String toCamelCase(String str) {
        if (isNull(str))
            return str;

        String result = str;
        String conv = str.replaceAll("\t", " ");

        if (conv.indexOf(" ") != -1) {
            String[] words = conv.split(" ");

            if (words.length > 1) {
                result = words[0];

                for (int i = 1; i < words.length; i++) {
                    String current = words[i].trim();

                    if (!isNull(current))
                        result += current.substring(0, 1).toUpperCase()
                                + current.substring(1);
                }
            }
        } else
            return str;

        return result;
    }

    /**
     * Compares a string to null, "null" or just with empty string
     * 
     * @param String
     *            -- input string
     * @return boolean -- true if comparison succeeds, otherwise false.
     */
    public static boolean isNull(String str) {
        return ((str == null) || ("".equals(str)) || ("null".equals(str)));
    }
}

Related

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