Java String Split split(String source, String seperator)

Here you can find the source of split(String source, String seperator)

Description

Splits a string into different parts, using a seperator string to detect the seperation boundaries in a case-sensitive manner.

License

CDDL license

Parameter

Parameter Description
source The string that will be split into parts.
seperator The seperator string that will be used to determine the parts.

Return

An ArrayList containing the parts as String objects.

Declaration

public static ArrayList split(String source, String seperator) 

Method Source Code


//package com.java2s;
/*/*from   w  w  w.j a v a2 s . c  om*/
 * Copyright 2001-2006 Geert Bevin <gbevin[remove] at uwyn dot com>
 * Distributed under the terms of either:
 * - the common development and distribution license (CDDL), v1.0; or
 * - the GNU Lesser General Public License, v2.1 or later
 * $Id: StringUtils.java 3108 2006-03-13 18:03:00Z gbevin $
 */

import java.util.ArrayList;

public class Main {
    /**
     * Splits a string into different parts, using a seperator string to detect
     * the seperation boundaries in a case-sensitive manner. The seperator will
     * not be included in the list of parts.
     *
     * @param source The string that will be split into parts.
     * @param seperator The seperator string that will be used to determine the
     * parts.
     *
     * @return An <code>ArrayList</code> containing the parts as
     * <code>String</code> objects.
     *
     * @since 1.0
     */
    public static ArrayList split(String source, String seperator) {
        return split(source, seperator, true);
    }

    /**
     * Splits a string into different parts, using a seperator string to detect
     * the seperation boundaries. The seperator will not be included in the list
     * of parts.
     *
     * @param source The string that will be split into parts.
     * @param seperator The seperator string that will be used to determine the
     * parts.
     * @param matchCase A <code>boolean</code> indicating if the match is going
     * to be performed in a case-sensitive manner or not.
     *
     * @return An <code>ArrayList</code> containing the parts as
     * <code>String</code> objects.
     *
     * @since 1.0
     */
    public static ArrayList split(String source, String seperator, boolean matchCase) {
        ArrayList substrings = new ArrayList();

        if (null == source) {
            return substrings;
        }

        if (null == seperator) {
            substrings.add(source);
            return substrings;
        }

        int current_index = 0;
        int delimiter_index = 0;
        String element = null;

        String source_lookup_reference = null;
        if (!matchCase) {
            source_lookup_reference = source.toLowerCase();
            seperator = seperator.toLowerCase();
        } else {
            source_lookup_reference = source;
        }

        while (current_index <= source_lookup_reference.length()) {
            delimiter_index = source_lookup_reference.indexOf(seperator, current_index);

            if (-1 == delimiter_index) {
                element = new String(source.substring(current_index, source.length()));
                substrings.add(element);
                current_index = source.length() + 1;
            } else {
                element = new String(source.substring(current_index, delimiter_index));
                substrings.add(element);
                current_index = delimiter_index + seperator.length();
            }
        }

        return substrings;
    }
}

Related

  1. split(String s, String spliter, boolean removeEmptyItem)
  2. split(String s, String splitStr)
  3. split(String s, String splitter)
  4. split(String self)
  5. split(String signature, boolean skipInitialAngleBracket)
  6. split(String str)
  7. split(String str)
  8. split(String str)
  9. split(String str)