Java String Camel Case To camelCaseToHuman(String input)

Here you can find the source of camelCaseToHuman(String input)

Description

Turns a CamelCase String to a more human friendly string See TestStringUtils for specification.

License

Open Source License

Parameter

Parameter Description
input a parameter

Declaration

public static String camelCaseToHuman(String input) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (c) 2008-2013, Linagora//from   ww  w. j  a  v a 2s.c om
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *       Linagora - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Turns a CamelCase String to a more human friendly string
     * See TestStringUtils for specification.
     * @param input
     * @return
     */
    public static String camelCaseToHuman(String input) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < input.length(); i++) {
            char currentChar = input.charAt(i);
            if (i == 0) {
                builder.append(Character.toUpperCase(currentChar));
            } else if (Character.isLowerCase(currentChar)) {
                builder.append(currentChar);
            } else if (Character.isUpperCase(currentChar)) {
                char previousChar = input.charAt(i - 1);
                if (Character.isUpperCase(previousChar)) { // most probably an acronym
                    builder.append(currentChar);
                    if (i < input.length() - 1) {
                        char nextChar = input.charAt(i + 1);
                        if (!Character.isUpperCase(nextChar)) { // current is last letter of acronym
                            if (builder.charAt(builder.length() - 1) != ' ') {
                                builder.append(' ');
                            }
                        }
                    }
                } else if (!Character.isUpperCase(previousChar)) {
                    if (builder.charAt(builder.length() - 1) != ' ') {
                        builder.append(' ');
                    }
                    if (i == input.length() - 1) {
                        builder.append(currentChar);
                    } else {
                        char nextChar = input.charAt(i + 1);
                        if (Character.isUpperCase(nextChar)) { // most probably an acronym
                            builder.append(currentChar);
                        } else {
                            builder.append(Character.toLowerCase(currentChar));
                        }
                    }
                }
            } else if (Character.isDigit(currentChar)) {
                char previousChar = input.charAt(i - 1);
                if (!Character.isDigit(previousChar)) { // first digit of a sequence
                    if (builder.charAt(builder.length() - 1) != ' ') {
                        builder.append(' ');
                    }
                }
                builder.append(currentChar);
                if (i < input.length() - 1) {
                    char nextChar = input.charAt(i + 1);
                    if (!Character.isDigit(nextChar)) { // current is last digit of the sequence
                        if (builder.charAt(builder.length() - 1) != ' ') {
                            builder.append(' ');
                        }
                    }
                }
            } else {
                builder.append(currentChar);
            }
        }

        return builder.toString();
    }
}

Related

  1. camelCaseToDashLowerName(String name)
  2. camelCaseToDashSeperated(String s)
  3. camelCaseToDotNotation(String string)
  4. camelCaseToEnum(String camelCase)
  5. camelCaseToEnum(String name)
  6. camelCaseToPhrase(String camelCase)
  7. camelCaseToPretty(String camelCase)
  8. camelCaseToSeparatedWords(String ccString, String separator)
  9. camelCaseToSeparatorCase(String s, String separator)