Java String Split splitOnTokens(String text)

Here you can find the source of splitOnTokens(String text)

Description

Splits a string into a number of tokens.

License

Open Source License

Parameter

Parameter Description
text the text to split

Return

the array of tokens, never null

Declaration

static String[] splitOnTokens(String text) 

Method Source Code

//package com.java2s;
// BSD-style license that can be found in the LICENSE file.

import java.util.ArrayList;

public class Main {
    /**/*from   ww w  .j av a2 s.  c  om*/
     * Splits a string into a number of tokens. The text is split by '?' and '*'. Where multiple '*'
     * occur consecutively they are collapsed into a single '*'.
     * 
     * @param text the text to split
     * @return the array of tokens, never null
     */
    static String[] splitOnTokens(String text) {
        // used by wildcardMatch
        // package level so a unit test may run on this

        if (text.indexOf('?') == -1 && text.indexOf('*') == -1) {
            return new String[] { text };
        }

        char[] array = text.toCharArray();
        ArrayList<String> list = new ArrayList<String>();
        StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (array[i] == '?' || array[i] == '*') {
                if (buffer.length() != 0) {
                    list.add(buffer.toString());
                    buffer.setLength(0);
                }
                if (array[i] == '?') {
                    list.add("?");
                } else if (list.isEmpty() || i > 0 && list.get(list.size() - 1).equals("*") == false) {
                    list.add("*");
                }
            } else {
                buffer.append(array[i]);
            }
        }
        if (buffer.length() != 0) {
            list.add(buffer.toString());
        }

        return list.toArray(new String[list.size()]);
    }
}

Related

  1. splitNames(final String string)
  2. splitObjectString(String str)
  3. splitOgnl(String ognl)
  4. splitOn(String toSplit, String splitter)
  5. splitOnNoWiki(String s)
  6. splitOnTokens(String text)
  7. splitOrderedDurationsIntoIntervals(String[] durations, int numberOfIntervals)
  8. splitPackageName(String packageName)
  9. splitPackages(String packages)