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

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

Description

Faster implementation of string splitting on a single character delimiter.

License

Open Source License

Parameter

Parameter Description
src the string to split
delim the delimiter character

Return

a String[] value

Declaration

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

Method Source Code

//package com.java2s;

import java.util.ArrayList;

public class Main {
    /**//ww w .  j  a  va2 s .c  om
     * Faster implementation of string splitting on a single character delimiter.
     * @param src the string to split
     * @param delim the delimiter character
     * @return a <code>String[]</code> value
     */
    public static String[] split(String src, char delim) {
        return split(src, delim, 0);
    }

    /**
     * Faster implementation of string splitting on a single character delimiter.
     * @param src the string to split
     * @param delim the delimiter character
     * @param limit return at most limit - 1 entries, 0 or below for as many as necessary.
     * @return a <code>String[]</code> value
     */
    public static String[] split(String src, char delim, int limit) {
        final ArrayList<String> output = new ArrayList<>();
        int index;
        int lindex = 0;
        final boolean limited = limit > 0;
        while ((index = src.indexOf(delim, lindex)) != -1 && (!limited || output.size() < limit - 1)) {
            output.add(src.substring(lindex, index));
            lindex = index + 1;
        }
        output.add(src.substring(lindex));
        return output.toArray(new String[output.size()]);
    }
}

Related

  1. split(String s, String delim)
  2. split(String s, String delimiter)
  3. split(String s, String delimiter)
  4. split(String s, String delimiter)
  5. split(String src, char delim)
  6. split(String src, String delimiter)
  7. split(String str, char delim)
  8. split(String str, char delimiter)
  9. split(String str, char delimiter)