Java String Split by Regex split(String input, String regex)

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

Description

Splits the input string with the given regex and filters empty strings.

License

Apache License

Parameter

Parameter Description
input the string to split.

Return

the array of strings computed by splitting this string

Declaration

public static String[] split(String input, String regex) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    /**//  www  .ja v  a2 s .co m
     * Splits the input string with the given regex and filters empty strings.
     *
     * @param input the string to split.
     * @return the array of strings computed by splitting this string
     */
    public static String[] split(String input, String regex) {
        if (input == null) {
            return null;
        }
        String[] arr = input.split(regex);
        List<String> results = new ArrayList<>(arr.length);
        for (String a : arr) {
            if (!a.trim().isEmpty()) {
                results.add(a);
            }
        }
        return results.toArray(new String[0]);
    }
}

Related

  1. split(final String regex, final String value)
  2. split(final String string, final String regex)
  3. split(String str, String regex)
  4. split(String str, String regex)
  5. split(String string, String pattern)
  6. split(String text, String pattern)