Java String Split by Comma splitCommaStr(String commaStr)

Here you can find the source of splitCommaStr(String commaStr)

Description

Given a comma-delimited string, split it into an array of strings, removing unneccessary whitespace.

License

Open Source License

Parameter

Parameter Description
commaStr a parameter

Return

an array of the strings, with leading and trailing whitespace removed.

Declaration

public static List<String> splitCommaStr(String commaStr) 

Method Source Code


//package com.java2s;

import java.util.ArrayList;
import java.util.Arrays;

import java.util.Iterator;
import java.util.List;

public class Main {
    /**/*from w ww  .ja va 2  s.  com*/
     * Given a comma-delimited string, split it into an array of
     * strings, removing unneccessary whitespace.  Also will remove
     * empty values (i.e. only whitespace).
     * 
     * @param commaStr 
     * @return an array of the strings, with leading and trailing 
     *         whitespace removed.
     */
    public static List<String> splitCommaStr(String commaStr) {
        List<String> results = new ArrayList<String>(Arrays.asList(commaStr.trim().split("\\s*,\\s*")));
        for (Iterator<String> it = results.iterator(); it.hasNext();) {
            String next = it.next();
            next = next.trim();
            if (next.equals("")) {
                it.remove();
            }
        }
        return results;
    }
}

Related

  1. splitCommand(String command)
  2. splitCommand(String command)
  3. splitCommand(String command)
  4. splitCommandString(String command_string)
  5. splitCommaSequence(String argumentString)
  6. splitDoubleParentCommas(String input)
  7. splitOnComma(String cs)
  8. splitWithCommaOrSemicolon(String src)