Java String Split by Delimiter splitOnEntireString(String target, String delimiter)

Here you can find the source of splitOnEntireString(String target, String delimiter)

Description

Break a string into pieces based on matching the full delimiter string in the text.

License

Open Source License

Parameter

Parameter Description
target The text to break up.
delimiter The sub-string which is used to break the target.

Return

List of String from the target.

Declaration

public static List splitOnEntireString(String target, String delimiter) 

Method Source Code

//package com.java2s;
/*/*from  w w w  .j  a v  a 2 s  . c o  m*/
 * JBoss, Home of Professional Open Source.
 *
 * See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing.
 *
 * See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Break a string into pieces based on matching the full delimiter string in the text. The delimiter is not included in the
     * returned strings.
     * 
     * @param target The text to break up.
     * @param delimiter The sub-string which is used to break the target.
     * @return List of String from the target.
     */
    public static List splitOnEntireString(String target, String delimiter) {
        ArrayList result = new ArrayList();
        if (delimiter.length() > 0) {
            int index = 0;
            int indexOfNextMatch = target.indexOf(delimiter);
            while (indexOfNextMatch > -1) {
                result.add(target.substring(index, indexOfNextMatch));
                index = indexOfNextMatch + delimiter.length();
                indexOfNextMatch = target.indexOf(delimiter, index);
            }
            if (index <= target.length()) {
                result.add(target.substring(index));
            }
        } else {
            result.add(target);
        }
        return result;
    }
}

Related

  1. splitHtmlTagKeepDelimiter(String tag, String input)
  2. splitKeepDelimiter(String delimiter, String input)
  3. splitList(String data, String delims)
  4. splitNestedString(String params, String delimStr, int numLeft, int numRight)
  5. splitNoCoalesce(String s, char delimiter)
  6. splitSimple(String delimiter, String str)
  7. splitSimpleLimit(String str, String delimiter, int limitSize)
  8. splitSqlScript(String script, char delim, List statements)
  9. splitStr(String str, char delimiter, boolean trim)