takes a delimited list and outputs the parts with the empty ones ignored - Java java.util

Java examples for java.util:List Value

Description

takes a delimited list and outputs the parts with the empty ones ignored

Demo Code


//package com.java2s;
import java.util.ArrayList;

public class Main {
    public static void main(String[] argv) {
        String input = "java2s.com";
        String regex = "o";
        System.out.println(splitStringIntoNonemptyParts(input, regex));
    }//from w  ww  .  j a va2s  . c  om

    /**
     * takes a delimited list and outputs the parts with the
     *       empty ones ignored
     * @param input      the delimited string
     * @param regex    the regular expression to use in splitting
     * @return         an array with the empty parts left out when splitting
     */
    public static ArrayList<String> splitStringIntoNonemptyParts(
            String input, String regex) {
        String[] theParts = input.split(regex);
        ArrayList<String> parts = new ArrayList<String>(theParts.length);
        for (String part : theParts) {
            if (!part.isEmpty()) {
                parts.add(part);
            }
        }
        return parts;
    }
}

Related Tutorials