Return an array with start/end indices for the characters used for camel case matching, ignoring the first (start) many camel case characters. - Java java.lang

Java examples for java.lang:String Camel Case

Description

Return an array with start/end indices for the characters used for camel case matching, ignoring the first (start) many camel case characters.

Demo Code

/**/*from   www  . j  av a 2 s. c om*/
 * Copyright (c) Red Hat, Inc., contributors and others 2013 - 2014. All rights reserved
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
//package com.java2s;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] argv) throws Exception {
        String s = "java2s.com";
        int start = 2;
        int length = 2;
        System.out.println(java.util.Arrays.toString(getCamelCaseIndices(s,
                start, length)));
    }

    /**
     * Return an array with start/end indices for the characters used for camel
     * case matching, ignoring the first (start) many camel case characters.
     * For example, getCamelCaseIndices("some CamelCase", 1, 2) will return
     * {{5,5},{10,10}}.
     * 
     * @param s the source string
     * @param start how many characters of getCamelCase(s) should be ignored
     * @param length for how many characters should indices be returned 
     * @return an array of length start
     */
    public static int[][] getCamelCaseIndices(String s, int start,
            int length) {
        List<int[]> result = new ArrayList<int[]>();
        int index = 0;
        while (start > 0) {
            index = getNextCamelIndex(s, index + 1);
            start--;
        }
        while (length > 0) {
            result.add(new int[] { index, index });
            index = getNextCamelIndex(s, index + 1);
            length--;
        }
        return result.toArray(new int[result.size()][]);
    }

    /**
     * Returns the next index to be used for camel case matching.
     * 
     * @param s the string
     * @param index the index
     * @return the next index, or -1 if not found
     */
    public static int getNextCamelIndex(String s, int index) {
        char c;
        while (index < s.length()
                && !(isSeparatorForCamelCase(c = s.charAt(index)))
                && Character.isLowerCase(c)) {
            index++;
        }
        while (index < s.length()
                && isSeparatorForCamelCase(c = s.charAt(index))) {
            index++;
        }
        if (index >= s.length()) {
            index = -1;
        }
        return index;
    }

    /**
     * Returns true if the given character is to be considered a separator
     * for camel case matching purposes.
     * 
     * @param c the character
     * @return true if the character is a separator
     */
    public static boolean isSeparatorForCamelCase(char c) {
        return !Character.isLetterOrDigit(c);
    }
}

Related Tutorials