Java String Split by Delimiter splitToList(String a_text, String a_delimiter)

Here you can find the source of splitToList(String a_text, String a_delimiter)

Description

Splits a line on a delimiter, removing any escape characters.

License

Open Source License

Parameter

Parameter Description
a_text the text string to be split
a_delimiter the delimiter to split on.

Return

a list of strings.

Declaration

public static List splitToList(String a_text, String a_delimiter) 

Method Source Code

//package com.java2s;
/*/*w  w  w  .j a  va  2s. com*/
 * @(#) StringUtils.java Jul 20, 2005
 * Copyright 2005 Frequency Marketing, Inc. All rights reserved.
 * Frequency Marketing, Inc. PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * The string escape character
     */
    public static final char ESCAPE_CHAR = '\\';

    /**
     * Splits a line on a delimiter, removing any escape characters.
     * @param a_text the text string to be split
     * @param a_delimiter the delimiter to split on.
     * @return a list of strings.
     */
    public static List splitToList(String a_text, String a_delimiter) {
        return splitToList(a_text, new String[] { a_delimiter });
    }

    /**
     * Split a string into segments delimited by any of the delimiters
     * specified in the a_delimiters array.
     * @param a_text
     * @param a_delimiters
     * @return a list of the strings found between each of the delimiters.
     */
    public static List splitToList(String a_text, String[] a_delimiters) {
        List partList = new ArrayList();
        if (null != a_text) {
            String part;
            int partStart = 0;
            int searchPos;
            int index = 0;
            while (index != -1) {
                int delimLength = -1;

                /*
                 * Find the next delimiter being sure to skip over any escaped
                 * delimiters that may exist.
                 */
                boolean foundDelimOrAtEnd = false;
                searchPos = partStart;
                while (!foundDelimOrAtEnd) {
                    int nextDelimiter;
                    int currentClosestDelimiter = Integer.MAX_VALUE;
                    for (int delimIndex = 0; delimIndex < a_delimiters.length; delimIndex++) {
                        String delimiter = a_delimiters[delimIndex];
                        nextDelimiter = a_text.indexOf(delimiter, searchPos);
                        if (nextDelimiter != -1 && nextDelimiter < currentClosestDelimiter) {
                            currentClosestDelimiter = nextDelimiter;
                            delimLength = delimiter.length();
                        }
                    }

                    index = -1;
                    if (currentClosestDelimiter != Integer.MAX_VALUE) {
                        index = currentClosestDelimiter;
                    }

                    if (index > 0) {
                        if (a_text.charAt(index - 1) == ESCAPE_CHAR) {
                            searchPos = index + delimLength;
                            index = -1;
                        } else {
                            foundDelimOrAtEnd = true;
                        }
                    } else {
                        foundDelimOrAtEnd = true;
                    }
                }

                if (index == -1) {
                    part = a_text.substring(partStart);
                    partList.add(removeEscape(part));
                } else {
                    part = a_text.substring(partStart, index);
                    partList.add(removeEscape(part));

                    partStart = index + delimLength;
                }
            }
        }

        return partList;
    }

    /**
     * Remove escape characters from the string. Any escape characters that are
     * themselves escaped end up being unescaped leaving a single escape
     * characters.
     * <pre>
     *  , = ,
     *  \, = ,
     *  \\ = \
     * </pre>
     * @param a_escaped the string to work on
     * @return the string without its escape characters
     */
    public static String removeEscape(String a_escaped) {
        String unescaped = null;

        if (null != a_escaped) {
            StringBuffer text = new StringBuffer();

            boolean foundEscapeChar = false;

            char character;
            int length = a_escaped.length();
            for (int index = 0; index < length; index++) {
                character = a_escaped.charAt(index);

                if (character == ESCAPE_CHAR) {
                    if (foundEscapeChar) {
                        text.append(character);
                        foundEscapeChar = false;
                    } else {
                        foundEscapeChar = true;
                    }
                } else {
                    text.append(character);
                    foundEscapeChar = false;
                }
            }

            unescaped = text.toString();
        }

        return unescaped;
    }
}

Related

  1. splitStringUsingDelimiter(String name, String delimiter)
  2. splitText(String text, String delimiter)
  3. splitToArray(String stringToSplit, String delimitter, boolean trim)
  4. splitToken(String str, String delimiter)
  5. splitToList(final String string, final String delim, final int limit)
  6. splitToList(String s, String delimRegEx)
  7. splitToList(String str, char delimiter)
  8. splitUpTo(String s, String delimiter, int cnt)
  9. splitWhileKeepingParentheses(String value, char delimiter)