split Camel Case String to Map - Java java.lang

Java examples for java.lang:String Camel Case

Description

split Camel Case String to Map

Demo Code

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Main {

  public static void main(String[] argv) {
    String s = "This is a TestTestTest java2s.com";
    System.out.println(splitCamelCase(s));
  }//from   www  .  ja v a2s. co  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