Java String Split by Delimiter split(String input, String... delimiters)

Here you can find the source of split(String input, String... delimiters)

Description

If no delimiters are supplied, one-element list containing input

License

Open Source License

Parameter

Parameter Description
input a parameter
delimiters a parameter

Declaration

public static List<String> split(String input, String... delimiters) 

Method Source Code


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

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**//from   w w  w. ja v a2  s  .c o m
     * If no delimiters are supplied, one-element list containing <code>input</code>
     * @param input
     * @param delimiters
     * @return
     */
    public static List<String> split(String input, String... delimiters) {
        if (input == null)
            return null;
        String str = input;
        List<String> list = new ArrayList<String>();

        if (delimiters == null || delimiters.length < 1) {
            list.add(input);
            return list;
        }

        String delim = delimiters[0];

        for (int i = 1; i < delimiters.length; i++) {
            str = str.replaceAll(delimiters[i], delim);
        }
        String[] tmp = str.split(delim);
        for (int i = 0; i < tmp.length; i++) {
            list.add(tmp[i].trim());
        }
        return list;
    }
}

Related

  1. split(String a, String delim)
  2. split(String input, char delimiter)
  3. split(String input, String delimiter)
  4. split(String input, String delimiter)
  5. split(String input, String... delimiters)
  6. split(String inputStr, String delimeter, String enclosureStr)
  7. split(String s, char delim)
  8. split(String s, char delim)
  9. split(String s, char delimiter)