Java String Split by Delimiter split(final String src, final char delim)

Here you can find the source of split(final String src, final char delim)

Description

Preserves null sections.

License

Open Source License

Parameter

Parameter Description
src The source string
delim The delimiter string to split by

Return

An array of strings.

Declaration

public static final String[] split(final String src, final char delim) 

Method Source Code


//package com.java2s;

import java.util.ArrayList;

public class Main {
    /**//from w  w  w  .j  a  va  2s. c om
     * Preserves null sections. "ab..cd" would produce [ab][][cd] with '.'
     * @param src The source string
     * @param delim The delimiter string to split by
     * @return An array of strings.
     */
    public static final String[] split(final String src, final char delim) {
        final ArrayList<String> output = new ArrayList<String>();
        int index = 0;
        int lindex = 0;
        while ((index = src.indexOf(delim, lindex)) != -1) {
            output.add(src.substring(lindex, index));
            lindex = index + 1;
        }
        output.add(src.substring(lindex));
        return output.toArray(new String[output.size()]);
    }

    /**
     * Preserves null sections. "ab..cd" would produce [ab][][cd] with "."
     * @param src The source string
     * @param delim The delimiter string to split by
     * @return An array of strings.
     */
    public static final String[] split(final String src, final String delim) {
        final ArrayList<String> output = new ArrayList<String>();
        final int len = delim.length();
        int index = 0;
        int lindex = 0;
        while ((index = src.indexOf(delim, lindex)) != -1) {
            output.add(src.substring(lindex, index));
            lindex = index + len;
        }
        output.add(src.substring(lindex));
        return output.toArray(new String[output.size()]);
    }
}

Related

  1. fastSplit(String string, char delimiter)
  2. fastSplit(String string, String delimiter)
  3. split(final boolean enable, final String value, final char delimiter)
  4. split(final String input, final char delimiter)
  5. split(final String input, final String delimiter, final boolean removeEmpty)
  6. split(final String str, final char delim)
  7. split(final String str, final String delimiter)
  8. split(String a, String delim)
  9. split(String input, char delimiter)