Java String Split by Delimiter split(String value, String delimiter)

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

Description

Splits the given String around the matches defined by the given delimiter into an array.

License

Open Source License

Parameter

Parameter Description
value the String which should be split into an array
delimiter the delimiter which marks the boundaries of the array

Return

an array, when the delimiter was not found, the array will only have a single element.

Declaration

public static String[] split(String value, String delimiter) 

Method Source Code


//package com.java2s;
/*// w  ww. j  a va 2s .c  om
 * Created on 24-Nov-2003 at 14:38:43
 *
 * Copyright (c) 2004-2005 Robert Virkus / Enough Software
 *
 * This file is part of J2ME Polish.
 *
 * J2ME Polish is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * J2ME Polish is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with J2ME Polish; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Commercial licenses are also available, please
 * refer to the accompanying LICENSE.txt or visit
 * http://www.j2mepolish.org for details.
 */

import java.util.ArrayList;

public class Main {
    /**
     * Splits the given String around the matches defined by the given delimiter into an array.
     * Example:
     * <pre>TextUtil.split("one;;two;;three", ";;")</pre> results into the array
     * <pre>{"one", "two", "three"}</pre>.
     *
     * @param value the String which should be split into an array
     * @param delimiter the delimiter which marks the boundaries of the array 
     * @return an array, when the delimiter was not found, the array will only have a single element.
     * @see #split(String, char) for an even faster alternative
     */
    public static String[] split(String value, String delimiter) {
        int lastIndex = 0;
        ArrayList strings = null;
        int currentIndex = 0;
        while ((currentIndex = value.indexOf(delimiter, lastIndex)) != -1) {
            if (strings == null) {
                strings = new ArrayList();
            }
            strings.add(value.substring(lastIndex, currentIndex));
            lastIndex = currentIndex + delimiter.length();
        }
        if (strings == null) {
            return new String[] { value };
        }
        // add tail:
        strings.add(value.substring(lastIndex));
        return (String[]) strings.toArray(new String[strings.size()]);
    }

    /**
     * Splits the given String around the matches defined by the given delimiter into an array.
     * Example:
     * <pre>TextUtil.split("one;two;three", ';')</pre> results into the array
     * <pre>{"one", "two", "three"}</pre>.
     *
     * @param value the String which should be split into an array
     * @param delimiter the delimiter which marks the boundaries of the array 
     * @return an array, when the delimiter was not found, the array will only have a single element.
     */
    public static String[] split(String value, char delimiter) {
        char[] valueChars = value.toCharArray();
        int lastIndex = 0;
        ArrayList strings = null;
        for (int i = 0; i < valueChars.length; i++) {
            char c = valueChars[i];
            if (c == delimiter) {
                if (strings == null) {
                    strings = new ArrayList();
                }
                strings.add(new String(valueChars, lastIndex, i - lastIndex));
                lastIndex = i + 1;
            }
        }
        if (strings == null) {
            return new String[] { value };
        }
        // add tail:
        strings.add(new String(valueChars, lastIndex, valueChars.length - lastIndex));
        return (String[]) strings.toArray(new String[strings.size()]);
    }
}

Related

  1. split(String strToSplit, char delimiter)
  2. split(String text, String delimiter)
  3. split(String value, char delim)
  4. split(String value, char delimiter)
  5. split(String value, String delimiter)
  6. split(String value, String[] delimiters)
  7. split(String what, char delim)
  8. split(String[] srcArray, String delimiterRegExp)
  9. split2(String string, String delimiter)