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

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

Description

splits the string based on the given delimiters.

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.*;

public class Main {
    /**// www  . j a v  a 2 s  .  c om
     * splits the string based on the given delimiters. 
     * 
     * 
     * @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>();

        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;
    }

    /**
     * will trim off the front and back of the string.
     * 
     * if trim = " " then it is the same as str.trim()
     * 
     * @param input
     * @param trim
     * @return
     */
    public static String trim(String input, String trim) {
        if (input == null || trim == null || trim.isEmpty())
            return input;

        String tmp = input;
        while (tmp.startsWith(trim)) {
            tmp = tmp.substring(trim.length());
        }
        while (tmp.endsWith(trim)) {
            tmp = tmp.substring(0, tmp.length() - trim.length());
        }
        return tmp;

    }
}

Related

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