Java String Camel Case to Underscore camelCaseToUnderscored(String camelCaseName)

Here you can find the source of camelCaseToUnderscored(String camelCaseName)

Description

A deliberately very inflexible camel case to underscored converter; it must not convert improper camel case names to a proper underscored name.

License

Apache License

Declaration

public static String camelCaseToUnderscored(String camelCaseName) 

Method Source Code

//package com.java2s;
/*/*from w w w .  ja  v  a 2s. co m*/
 * Copyright 2014 Attila Szegedi, Daniel Dekany, Jonathan Revusky
 * 
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 * 
 * 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 {
    /**
     * A deliberately very inflexible camel case to underscored converter; it must not convert improper camel case
     * names to a proper underscored name.
     */
    public static String camelCaseToUnderscored(String camelCaseName) {
        int i = 0;
        while (i < camelCaseName.length()
                && Character.isLowerCase(camelCaseName.charAt(i))) {
            i++;
        }
        if (i == camelCaseName.length()) {
            // No conversion needed
            return camelCaseName;
        }

        StringBuffer sb = new StringBuffer();
        sb.append(camelCaseName.substring(0, i));
        while (i < camelCaseName.length()) {
            final char c = camelCaseName.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append('_');
                sb.append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
            i++;
        }
        return sb.toString();
    }
}

Related

  1. camelCaseToUnderscore(final String camelCaseString)
  2. camelCaseToUnderscore(final String str)
  3. camelCaseToUnderscore(String camelCase)
  4. camelCaseToUnderscore(String camelCased)
  5. camelCaseToUnderScore(String key)
  6. camelCaseToUnderscores(String camel)
  7. camelCaseToUnderScoreUpperCase(String camelCase)
  8. camelToUnder(String value)
  9. camelToUnderline(String param)