Java String Camel Case camelCase(String s)

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

Description

Converts a string to camel case, assuming '_' as the word separator.

License

Open Source License

Parameter

Parameter Description
s The string to be camel cased.

Return

The camel cased string.

Declaration

public static String camelCase(String s) 

Method Source Code

//package com.java2s;
/*//from   www. j  av  a  2s . co m
    
Copyright: 2010 Bindley Bioscience Center, Purdue University
    
License: X11 license.
    
  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:
    
  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.
    
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
    
*/

public class Main {
    /**
     * Converts a string to camel case, assuming '_' as the word separator.
     * 
     * @param s The string to be camel cased.
     * @return The camel cased string.
     */
    public static String camelCase(String s) {
        return camelCase(s, '_');
    }

    /**
     * Converts a string to camel case.
     * 
     * @param s The string to be camel cased.
     * @param space The current word separator character.
     * @return The camel cased string.
     */
    public static String camelCase(String s, char space) {
        String[] words = s.split("" + space);
        StringBuilder returnValue = new StringBuilder(words[0]);
        for (int i = 1; i < words.length; i++) {
            if (words[i].length() >= 1)
                returnValue.append(words[i].substring(0, 1).toUpperCase());
            if (words[i].length() >= 2)
                returnValue.append(words[i].substring(1));
        }
        return returnValue.toString();
    }

    /**
     * Converts a character to a String.
     * 
     * @param c The character to be converted.
     * @return A string containing a single character.
     */
    public static String toString(char c) {
        return new StringBuilder().append(c).toString();
    }
}

Related

  1. camelCase(String inStr, int start, int end)
  2. camelCase(String key)
  3. camelCase(String name, boolean capitalize)
  4. camelCase(String s)
  5. camelCase(String s)
  6. camelCase(String s)
  7. camelCase(String s)
  8. camelCase(String str)
  9. CamelCase(String str)