convert Camel Case To All Caps - Java java.lang

Java examples for java.lang:String Camel Case

Introduction

The following code shows how to convert Camel Case To All Caps

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String qualifiedClassName = "AbcDefTest";
        System.out.println(convertCamelCaseToAllCaps(qualifiedClassName));
    }//from w  w w . j av  a2 s  . c o m

    public static String convertCamelCaseToAllCaps(String qualifiedClassName) {
        StringBuffer sb = new StringBuffer();
        final char[] charArray = qualifiedClassName.toCharArray();
        for (int c = 0; c < charArray.length; c++) {
            char character = charArray[c];
            final boolean isEnd = c + 1 == charArray.length;
            boolean nextIsUpper = !isEnd
                    && Character.isUpperCase(charArray[c + 1]);
            boolean nextNextIsLower = c + 2 < (charArray.length)
                    && Character.isLowerCase(charArray[c + 2]);

            sb.append(Character.toUpperCase(character));
            if (Character.isLowerCase(character) && nextIsUpper) {
                sb.append('_');
            } else {
                if (nextIsUpper && nextNextIsLower) {
                    sb.append('_');
                }
            }
        }
        return sb.toString();
    }
}

Related Tutorials