converts string to camel case, using the specified string delimiter - Java java.lang

Java examples for java.lang:String Camel Case

Description

converts string to camel case, using the specified string delimiter

Demo Code


//package com.java2s;

public class Main {


    /**//from  w  w  w.j a  v a 2  s .co  m
     * converts string to camel case, using the specified string delimiter
     */
    public static String camelCase(final String value, final String delim) {
        // check for null
        if (value == null || value.isEmpty()) {
            return "";
        }

        final StringBuilder result = new StringBuilder(value.length());
        final String[] parts = value.split(delim);
        if (parts != null && parts.length > 0) {
            boolean mustCapitalize = false;
            for (final String part : parts) {
                if (part.length() > 1) {
                    if (mustCapitalize) {
                        result.append(part.substring(0, 1).toUpperCase());
                        result.append(part.substring(1).toLowerCase());
                    } else {
                        result.append(part);
                        mustCapitalize = true;
                    }
                } else {
                    result.append(part);
                }
            }
        }

        return result.toString();
    }
}

Related Tutorials