Here you can find the source of stringToCollection(String string, String delim)
Parameter | Description |
---|---|
string | The string to be parsed |
delim | The delimiter to use |
public static ArrayList<String> stringToCollection(String string, String delim)
//package com.java2s; /* //from w ww . ja va 2 s.com * RealmSpeak is the Java application for playing the board game Magic Realm. * Copyright (c) 2005-2015 Robin Warren * E-mail: robin@dewkid.com * * This program is free software: you can redistribute it and/or modify it under the terms of the * GNU General Public License as published by the Free Software Foundation, either version 3 of the License, * or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. * * You should have received a copy of the GNU General Public License along with this program. If not, see * * http://www.gnu.org/licenses/ */ import java.util.*; public class Main { /** * This method will return all the values in a delimited String as a Collection of String objects. Where delimiters suggest a missing * value, an empty string will be returned. (i.e., the string ",,," would return four empty strings.) * * @param string The string to be parsed * @param delim The delimiter to use */ public static ArrayList<String> stringToCollection(String string, String delim) { return stringToCollection(string, delim, false); } public static ArrayList<String> stringToCollection(String string, String delim, boolean convertNulls) { ArrayList<String> list = new ArrayList<String>(); boolean stringAdded = false; StringTokenizer tokens = new StringTokenizer(string, delim, true); while (tokens.hasMoreTokens()) { String token = tokens.nextToken(); if (delim.indexOf(token) >= 0) { // found a delimiter if (!stringAdded) { list.add(""); } stringAdded = false; } else { // found a string if (convertNulls && "null".equals(token)) { token = null; } list.add(token); stringAdded = true; } } if (!stringAdded) { list.add(""); } return list; } }