split Camel Case - Java java.lang

Java examples for java.lang:String Camel Case

Introduction

The following code shows how to split Camel Case.

Demo Code

//package com.java2s;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Main {
    public static void main(String[] argv) {
        String s = "AbcDef";
        System.out.println(splitCamelCase(s));
    }/*from w w  w .j a v a 2 s .  c o m*/

    public static Map<String, String> done = new ConcurrentHashMap<String, String>(
            20, 0.9f, 2);
    public static final String CC_REPLACE = "(?<=[A-Z])(?=[A-Z][a-z])|(?<=[^A-Z])(?=[A-Z])|(?<=[A-Za-z])(?=[^A-Za-z])";

    public static String splitCamelCase(String s) {

        String displayString = done.get(s);

        if (displayString == null) {

            String caps = capitalize(s);

            if (caps.contains("_")) {
                caps = caps.replace("_", "");
            }
            displayString = caps.replaceAll(CC_REPLACE, " ");
            done.put(s, displayString);
        }
        return displayString;

    }

    public static String capitalize(String str) {

        if (str == null || str.isEmpty()) {
            return "";
        }

        char[] buffer = str.toCharArray();
        buffer[0] = Character.toTitleCase(buffer[0]);

        return new String(buffer);
    }
}

Related Tutorials