Java String Tokenize tokenize(String str, char delim)

Here you can find the source of tokenize(String str, char delim)

Description

tokenize

License

Apache License

Declaration

public static List<String> tokenize(String str, char delim) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    public static List<String> tokenize(String str, char delim) {
        ArrayList<String> result = new ArrayList<String>();
        StringBuilder current = new StringBuilder();
        boolean escaped = false;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c == delim) {
                if (escaped) {
                    current.append(c);//  w w  w . j  av  a  2s  . co m
                    escaped = false;
                } else {
                    escaped = true;
                }
            } else {
                if (escaped && current.length() > 0) {
                    result.add(current.toString());
                    current.setLength(0);
                    escaped = false;
                }
                current.append(c);
            }
        }
        if (current.length() > 0) {
            result.add(current.toString());
        }
        return result;
    }

    /**
     * Return a stringified version of the array.
     * @param array the array
     * @param delim the delimiter to use between array components
     * @return the string form of the array
     */
    public static String toString(final Object[] array, final String delim) {
        return toString(array, delim, true);
    }

    /**
     * Return a stringified version of the array.
     * @param array the array
     * @param delim the delimiter to use between array components
     * @return the string form of the array
     */
    public static String toString(final Object[] array, final String delim, boolean includeBrackets) {
        if (array == null) {
            return ""; //$NON-NLS-1$
        }
        final StringBuffer sb = new StringBuffer();
        if (includeBrackets) {
            sb.append('[');
        }
        for (int i = 0; i < array.length; ++i) {
            if (i != 0) {
                sb.append(delim);
            }
            sb.append(array[i]);
        }
        if (includeBrackets) {
            sb.append(']');
        }
        return sb.toString();
    }

    /**
     * Return a stringified version of the array, using a ',' as a delimiter
     * @param array the array
     * @return the string form of the array
     * @see #toString(Object[], String)
     */
    public static String toString(final Object[] array) {
        return toString(array, ",", true); //$NON-NLS-1$
    }
}

Related

  1. tokenize(String s1)
  2. Tokenize(String sent)
  3. tokenize(String sentence)
  4. tokenize(String source, char separator)
  5. tokenize(String source, char separator)
  6. tokenize(String str, String delims)
  7. tokenize(String str, String delims, boolean returnDelims)
  8. tokenize(String string, String splitOn)
  9. tokenize(String text)