Java Comma Separated List commaDelimitedListToStringArray(String str)

Here you can find the source of commaDelimitedListToStringArray(String str)

Description

comma Delimited List To String Array

License

Apache License

Declaration

public static String[] commaDelimitedListToStringArray(String str) 

Method Source Code


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

import java.util.ArrayList;

import java.util.Collection;

import java.util.List;

public class Main {
    public static String[] commaDelimitedListToStringArray(String str) {
        return delimitedListToStringArray(str, ",");
    }/* w w  w  . j  a  va 2s. com*/

    public static String[] delimitedListToStringArray(String str, String delimiter) {
        if (str == null)
            return new String[0];
        if (delimiter == null) {
            return new String[] { str };
        }
        List result = new ArrayList();
        if ("".equals(delimiter)) {
            for (int i = 0; i < str.length(); i++)
                result.add(str.substring(i, i + 1));
        } else {
            int pos = 0;
            int delPos = 0;
            while ((delPos = str.indexOf(delimiter, pos)) != -1) {
                result.add(str.substring(pos, delPos));
                pos = delPos + delimiter.length();
            }
            if ((str.length() > 0) && (pos <= str.length())) {
                result.add(str.substring(pos));
            }
        }
        return toStringArray(result);
    }

    public static String[] toStringArray(Collection collection) {
        if (collection == null)
            return null;
        return (String[]) collection.toArray(new String[collection.size()]);
    }
}

Related

  1. addPropertyToCommand(List command, String key, String value)
  2. commaDelimitedListToStringArray(final String str)
  3. commaDelimitedListToStringArray(String str)
  4. commaDelimitedListToStringArray(String str)
  5. commaDelimitedListToStringArray(String str)
  6. commaDelimitedStringToList(String str)
  7. commaDelimitedToList(String commaDelimited)
  8. commalist(List l)
  9. commalist(List l)