Java String Camel Case to Underscore camelCaseToUnderscore(String camelCase)

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

Description

Turns CamelCased string into Underscored one

License

Open Source License

Parameter

Parameter Description
camelCase a parameter

Declaration

public static String camelCaseToUnderscore(String camelCase) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 - 2014 DigiArea, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*  w  w  w.  j a v a 2s  . c  o m*/
 *     DigiArea, Inc. - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Turns CamelCased string into Underscored one
     * 
     * @param camelCase
     * @return
     */
    public static String camelCaseToUnderscore(String camelCase) {
        StringBuilder builder = new StringBuilder();
        char[] chars = camelCase.toCharArray();
        for (int j = 0; j < chars.length; j++) {
            if (Character.isUpperCase(chars[j])) {
                if (j != 0) {
                    builder.append('_');
                }
                builder.append(Character.toLowerCase(chars[j]));
            } else {
                builder.append(chars[j]);
            }
        }
        return builder.toString();
    }
}

Related

  1. camelCase2UnderscoreLowercase(String name)
  2. camelCaseToLowerCaseUnderline(String name)
  3. camelCaseToUnderline(String name)
  4. camelCaseToUnderscore(final String camelCaseString)
  5. camelCaseToUnderscore(final String str)
  6. camelCaseToUnderscore(String camelCased)
  7. camelCaseToUnderScore(String key)
  8. camelCaseToUnderscored(String camelCaseName)
  9. camelCaseToUnderscores(String camel)