camel Case To Upper Snake Case - Java java.lang

Java examples for java.lang:String Camel Case

Description

camel Case To Upper Snake Case

Demo Code

/**//from  w  w  w .  jav a  2 s  .  co m
 * Copyright (C) 2010-2014 eBusiness Information, Excilys Group
 *
 * 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.
 */
//package com.java2s;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static final Pattern pattern = Pattern
            .compile("([A-Z]|[a-z])[a-z]*");

    public static String camelCaseToUpperSnakeCase(String camelCase) {
        return camelCaseToSnakeCase(camelCase).toUpperCase();
    }

    public static String camelCaseToUpperSnakeCase(String prefix,
            String camelCase, String suffix) {
        if (prefix != null && !camelCase.startsWith(prefix)) {
            camelCase = prefix + "_" + camelCase;
        }
        if (suffix != null
                && !camelCase.toLowerCase().endsWith(suffix.toLowerCase())) {
            camelCase = camelCase + "_" + suffix;
        }
        return camelCaseToUpperSnakeCase(camelCase);
    }

    public static String camelCaseToSnakeCase(String camelCase) {

        List<String> tokens = new ArrayList<String>();
        Matcher matcher = pattern.matcher(camelCase);
        String acronym = "";
        while (matcher.find()) {
            String found = matcher.group();
            if (found.matches("^[A-Z]$")) {
                acronym += found;
            } else {
                if (acronym.length() > 0) {
                    // we have an acronym to add before we continue
                    tokens.add(acronym);
                    acronym = "";
                }
                tokens.add(found.toLowerCase());
            }
        }
        if (acronym.length() > 0) {
            tokens.add(acronym);
        }
        if (tokens.size() > 0) {
            StringBuilder sb = new StringBuilder(tokens.remove(0));
            for (String s : tokens) {
                sb.append("_").append(s);
            }
            return sb.toString();
        } else {
            return camelCase;
        }
    }
}

Related Tutorials