Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to Convert a string from camelCase to Title Case

807603Jan 3 2008 — edited Jan 3 2008
The following code may be of some use for somebody.
 /**
     * Converts the string with a camel case or with underscores and replace it 
     * with spaces between each word, and underscores replaced by spaces.
     * For example "javaProgramming" and "JAVA_PROGRAMMING"
     * would both become Java Programming".
     * @param str The String to convert
     * @return The converted String
     */
    public static String toTitleCase( String str )
    {
        if( str == null || str.length() == 0 )
        {
            return str;
        }

        StringBuffer result = new StringBuffer();

        /*
         * Pretend space before first character
         */
        char prevChar = ' ';

        /*
         * Change underscore to space, insert space before capitals
         */
        for( int i = 0; i < str.length(); i++ )
        {
            char c = str.charAt( i );
            if( c == '_' )
            {
                result.append( ' ' );
            }
            else if( prevChar == ' ' || prevChar == '_' )
            {
                result.append( Character.toUpperCase( c ) );
            }
            else if( Character.isUpperCase( c ) && !Character.isUpperCase( prevChar ) )
            {
                /*
                 * Insert space before start of word if camel case
                 */
                result.append( ' ' );
                result.append( Character.toUpperCase( c ) );
            }
            else
            {
                result.append( c );
            }

            prevChar = c;
        }

        return result.toString();
    }{code}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 31 2008
Added on Jan 3 2008
2 comments
1,787 views