Java String Split by Word splitIdentifierToWords(String str)

Here you can find the source of splitIdentifierToWords(String str)

Description

Splits indentifier into words

License

Open Source License

Declaration

public static String[] splitIdentifierToWords(String str) 

Method Source Code

//package com.java2s;
/*/*from w  ww.j a v a  2 s .  c o  m*/
 * Copyright 2001-2008 Aqris Software AS. All rights reserved.
 * 
 * This program is dual-licensed under both the Common Development
 * and Distribution License ("CDDL") and the GNU General Public
 * License ("GPL"). You may elect to use one or the other of these
 * licenses.
 */

import java.util.ArrayList;

public class Main {
    public static final String[] NO_STRINGS = new String[0];

    /**
     * Splits indentifier into words
     * @refactoring maybe we shall refactor insertDelimitersIntoCamelStyleNotation 
     *  to return array of words already. 
     */
    public static String[] splitIdentifierToWords(String str) {
        str = insertDelimitersIntoCamelStyleNotation(str, '_');
        return str.split("_");
    }

    /**
     * A function that will insert Your custom dilimiter between words, that are
     * written in so called 'camel notation'.<br><br>
     * For example: 'someVariableABCName' -> 'some[?]Variable[?]ABC[?]Name', where
     * '[?]' -- is Your custom dilimiter.<br><br>
     * See also splitCamelStyleIntoWords(String) (inserts spaces) and
     * getUpercaseStyleName(String) (inserts underscores).
     */
    public static final String insertDelimitersIntoCamelStyleNotation(String str, char dilimiter) {
        StringBuffer temp = new StringBuffer(str);
        boolean prevUpper = false;
        boolean curUpper = false;
        boolean nextUpper = false;
        int ofst = 0;
        for (int i = 0; i < str.length(); i++) {
            if (i == 0 && (Character.isUpperCase(str.charAt(i)))) {
                curUpper = true;
            } else {
                prevUpper = curUpper;
                curUpper = nextUpper;
            }

            nextUpper = i + 1 < str.length() && Character.isUpperCase(str.charAt(i + 1));

            if (!curUpper && str.charAt(i) != dilimiter && nextUpper && i + 1 < str.length()) {
                temp.insert(i + ofst + 1, dilimiter);
                ofst++;
            }
        }

        return new String(temp);
    }

    /**
     * Splits string into strings array separating it with comma
     * @param toSplit string to split
     * @return String array
     */
    public static String[] split(final String toSplit) {
        return split(toSplit, ",");
    }

    /**
     * Splits strings into strings array separating it with given splitter
     * @param toSplit string to split
     * @param splitter separator in string
     * @return String array
     */
    public static String[] split(final String toSplit, final String splitter) {
        if (toSplit == null) {
            return NO_STRINGS;
        }
        final ArrayList retVal = new ArrayList();
        int splitPos = toSplit.indexOf(splitter);
        int lastPos = 0;
        while (splitPos > -1) {
            retVal.add(toSplit.substring(lastPos, splitPos));
            lastPos = splitPos + splitter.length();
            if (lastPos >= toSplit.length()) {
                break;
            }
            splitPos = toSplit.indexOf(splitter, lastPos);
        }
        if (lastPos < toSplit.length()) {
            retVal.add(toSplit.substring(lastPos));
        } else if (lastPos == toSplit.length()) {
            retVal.add("");
        }
        return (String[]) retVal.toArray(new String[retVal.size()]);
    }
}

Related

  1. split(String words, String character)
  2. splitIntoWords(String s)
  3. splitKeyWords(String sql)
  4. splitOne(String wordString)
  5. splitUnit(String word)