Java String Split by Separator splitByStringSeparator(String theString, String separatorString)

Here you can find the source of splitByStringSeparator(String theString, String separatorString)

Description

This method tokenizes a given string using another given string as separator and returns a List containing found tokens.

License

Open Source License

Parameter

Parameter Description
theString the <tt>String</tt> to be tokenized.
separatorString the <tt>String</tt> separator between tokens.

Return

a List containing found tokens.

Declaration

public static List<String> splitByStringSeparator(String theString, String separatorString) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009, 2016 GreenVulcano ESB Open Source Project.
 * All rights reserved.// w  w  w .ja  v a2  s .  c o m
 *
 * This file is part of GreenVulcano ESB.
 *
 * GreenVulcano ESB is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GreenVulcano ESB 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with GreenVulcano ESB. If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * This method tokenizes a given string using another given string as
     * separator and returns a <tt>List</tt> containing found tokens. The
     * returned <tt>List</tt> is NEVER null (it may have zero elements, anyway).
     * 
     * @param theString
     *        the <tt>String</tt> to be tokenized.
     * @param separatorString
     *        the <tt>String</tt> separator between tokens.
     * @return a <tt>List</tt> containing found tokens.
     */
    public static List<String> splitByStringSeparator(String theString, String separatorString) {
        List<String> tokenList = new ArrayList<String>();

        if ((theString == null) || theString.equals("")) {
            return tokenList;
        }
        if (theString.equals(separatorString)) {
            return tokenList;
        }
        if ((separatorString == null) || separatorString.equals("")) {
            tokenList.add(theString);
            return tokenList;
        }
        if (theString.indexOf(separatorString) == -1) {
            tokenList.add(theString);
            return tokenList;
        }

        /*if (separatorString.length() == 1) {
        StringTokenizer st = new StringTokenizer(theString, separatorString);
        while (st.hasMoreTokens()) {
        String currToken = st.nextToken();
        tokenList.add(currToken);
        }
        return tokenList;
        }*/

        int startNextToken = 0;
        int endNextToken = 0;
        int maxPosition = theString.length();

        while (startNextToken < maxPosition) {
            endNextToken = theString.indexOf(separatorString, startNextToken);
            if (endNextToken != -1) {
                if (endNextToken > startNextToken) {
                    String currToken = theString.substring(startNextToken, endNextToken);
                    tokenList.add(currToken);
                } else if (endNextToken == startNextToken) {
                    tokenList.add("");
                }
                startNextToken = endNextToken + separatorString.length();
            } else if (startNextToken <= (maxPosition - 1)) {
                String currToken = theString.substring(startNextToken);
                tokenList.add(currToken);
                startNextToken = maxPosition;
            }
        }
        return tokenList;
    }
}

Related

  1. split(String strToSplit, String strSeparator, int iLimit)
  2. split(String text, String separators, String brackets)
  3. splitAll(String str, char separatorChar)
  4. splitAndTrim(String tags, String separator)
  5. splitBySeparator(String path, String separator)
  6. splitDoubles(String str, String fieldSeparator)
  7. splitField(String fieldWithSeparator, String separator)
  8. splitInts(String string, String separator)
  9. splitList(String source, char separator)