Java String Split split(final String toSplit)

Here you can find the source of split(final String toSplit)

Description

Splits string into strings array separating it with comma

License

Open Source License

Parameter

Parameter Description
toSplit string to split

Return

String array

Declaration

public static String[] split(final String toSplit) 

Method Source Code

//package com.java2s;
/*/*from   w  ww . j a  va  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 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(final String str)
  2. split(final String string)
  3. split(final String string)
  4. split(final String string, final String toSplit)
  5. split(final String text, final String sp)
  6. split(Iterable objects, boolean even)
  7. split(String arg)
  8. split(String candidate)
  9. split(String cmd)