Java String Camel Case to Upper Case camelToUpper(String s)

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

Description

Converts a camel-case name to an upper-case name with underscores.

License

Open Source License

Parameter

Parameter Description
s Camel-case string

Return

Upper-case string

Declaration

public static String camelToUpper(String s) 

Method Source Code

//package com.java2s;
/*//  w  ww. j a v  a  2  s. c  om
// This software is subject to the terms of the Eclipse Public License v1.0
// Agreement, available at the following URL:
// http://www.eclipse.org/legal/epl-v10.html.
// You must accept the terms of that agreement to use this software.
//
// Copyright (C) 2001-2005 Julian Hyde
// Copyright (C) 2005-2012 Pentaho and others
// All Rights Reserved.
*/

public class Main {
    /**
     * Converts a camel-case name to an upper-case name with underscores.
     *
     * <p>For example, <code>camelToUpper("FooBar")</code> returns "FOO_BAR".
     *
     * @param s Camel-case string
     * @return  Upper-case string
     */
    public static String camelToUpper(String s) {
        StringBuilder buf = new StringBuilder(s.length() + 10);
        int prevUpper = -1;
        for (int i = 0; i < s.length(); ++i) {
            char c = s.charAt(i);
            if (Character.isUpperCase(c)) {
                if (i > prevUpper + 1) {
                    buf.append('_');
                }
                prevUpper = i;
            } else {
                c = Character.toUpperCase(c);
            }
            buf.append(c);
        }
        return buf.toString();
    }
}

Related

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