Java String Split by Char split(char elem, String orig)

Here you can find the source of split(char elem, String orig)

Description

split on the given character, with the resulting tokens not including that character

License

Open Source License

Return

non-null

Declaration

public static final String[] split(char elem, String orig) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**//from ww w  .  j a v a2  s. c o m
     * split on the given character, with the resulting tokens not including that character
     * @return non-null
     */
    public static final String[] split(char elem, String orig) {
        return split("" + elem, orig);
    }

    /**
     * split on all of the characters in splitElements, with the resulting tokens not including that character
     * @return non-null
     */
    public static final String[] split(String splitElements, String orig) {
        return split(splitElements, orig, true);
    }

    /**
     * @return non-null
     */
    public static final String[] split(String splitElements, String orig, boolean includeZeroSizedElem) {
        List vals = new ArrayList();
        int off = 0;
        int start = 0;
        char str[] = orig.toCharArray();
        while (off < str.length) {
            if (splitElements.indexOf(str[off]) != -1) {
                String val = null;
                if (off - start > 0) {
                    val = new String(str, start, off - start);
                } else {
                    val = "";
                }
                if ((val.trim().length() > 0) || (includeZeroSizedElem))
                    vals.add(val);
                start = off + 1;
            }
            off++;
        }
        String val;
        if (off - start > 0)
            val = new String(str, start, off - start);
        else
            val = "";
        if ((val.trim().length() > 0) || (includeZeroSizedElem))
            vals.add(val);
        String rv[] = new String[vals.size()];
        for (int i = 0; i < rv.length; i++)
            rv[i] = (String) vals.get(i);
        return rv;
    }
}

Related

  1. fastSplit(final String string, final char sep)
  2. fastSplitTrimmed(final String string, final char sep)
  3. safeSplit(String string, char divider)
  4. split(char c, String s)
  5. split(char sep, String input)
  6. split(final String input, final char split)
  7. split(final String string, final char... toSplit)
  8. split(String cs, char sep)