Java String Camel Case to Upper Case camelCaseToUpperCase(String camelCaseName)

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

Description

camel Case To Upper Case

License

Apache License

Parameter

Parameter Description
camelCaseName a parameter

Declaration

public static String camelCaseToUpperCase(String camelCaseName) 

Method Source Code

//package com.java2s;
/*//from  w  w w.j a v a  2  s.  c  o  m
 * Copyright 2012 the original author or authors.
 *
 * 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 {
    /**
    *
    * @param camelCaseName
    * @return
    */
    public static String camelCaseToUpperCase(String camelCaseName) {
        StringBuilder upperCaseName = new StringBuilder();

        boolean prevCharIsNumber = false;

        for (int i = 0; i < camelCaseName.length(); i++) {
            char c = camelCaseName.charAt(i);

            if (i > 0 && c >= 'A' && c <= 'Z') {
                upperCaseName.append("_").append(c);
                prevCharIsNumber = false;
            } else if (c >= '0' && c <= '9') {
                if (prevCharIsNumber) {
                    upperCaseName.append(c);
                } else {
                    upperCaseName.append('_').append(c);
                    prevCharIsNumber = true;
                }
            } else {
                prevCharIsNumber = false;
                upperCaseName.append(Character.toUpperCase(c));
            }
        }

        return upperCaseName.toString();
    }
}

Related

  1. camelCaseToUpperCase(final String camelCase)
  2. camelCaseToUpperCase(String in)
  3. camelToUpper(String camel)
  4. camelToUpper(String name)
  5. camelToUpper(String s)