Java String Split by Case splitCamelCase(final String in)

Here you can find the source of splitCamelCase(final String in)

Description

Splits camel case.

License

Apache License

Declaration

public static List<String> splitCamelCase(final String in) 

Method Source Code

//package com.java2s;
/**/*ww w  . ja  v a 2s .c  o m*/
 * Copyright 2008 Matthew Hillsdon
 *
 * 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.
 */

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Splits camel case.
     *
     * Behaviour undefined for strings containing punctuation/whitespace.
     * Note that here uppercase is defined as !Character.isLowerCase(char)
     * notably, this includeds digits.
     */
    public static List<String> splitCamelCase(final String in) {
        List<String> result = new ArrayList<String>();
        char[] chars = in.toCharArray();
        int takenLength = 0;
        boolean lastLower = false;
        boolean nextLower = isNextLower(chars, -1);
        boolean lastNeutral = false;
        boolean currentNeutral = false;
        for (int i = 0; i < chars.length; ++i) {
            boolean currentUpper = !nextLower;
            boolean lastUpper = !lastLower && i > 0;
            nextLower = isNextLower(chars, i);
            currentNeutral = isCurrentNeutral(chars, i);

            // Step up is e.g. aB, step down is Ab.
            boolean stepUp = lastLower && (currentUpper && !currentNeutral);
            boolean nextStepDown = (lastUpper && !lastNeutral) && (currentUpper && !currentNeutral) && nextLower;
            if (stepUp || nextStepDown) {
                result.add(in.substring(takenLength, i));
                takenLength = i;
            }
            lastLower = !currentUpper;
            lastNeutral = currentNeutral;
        }
        result.add(in.substring(takenLength));
        return result;
    }

    private static boolean isNextLower(final char[] chars, final int currentPos) {
        if (currentPos + 1 < chars.length) {
            char c = chars[currentPos + 1];
            return Character.isLowerCase(c);
        }
        return false;
    }

    private static boolean isCurrentNeutral(final char[] chars, final int currentPos) {
        if (currentPos < chars.length) {
            char c = chars[currentPos];
            return !Character.isLetterOrDigit(c);
        }
        return false;
    }
}

Related

  1. split(String pString, String pSeparator, boolean pIgnoreCase)
  2. splitByCamelCase(String name)
  3. splitByCharacterTypeCamelCase(String targetName)
  4. splitCamelCase(final String s)
  5. splitCamelCase(String src)
  6. splitCamelCase(String string)
  7. splitOnUppercase(String input)