Here you can find the source of explode(String delim, String string)
Parameter | Description |
---|---|
delim | the delimiter |
string | the string to split |
public static String[] explode(String delim, String string)
//package com.java2s; /**/*from w w w . ja v a2s . co m*/ * This file is part of CubeEngine. * CubeEngine is licensed under the GNU General Public License Version 3. * * CubeEngine 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. * * CubeEngine 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 CubeEngine. If not, see <http://www.gnu.org/licenses/>. */ import java.util.ArrayList; import java.util.List; public class Main { /** * This method splits a string without RegExes * * @param delim the delimiter * @param string the string to split * @return an array containing the parts */ public static String[] explode(String delim, String string) { return explode(delim, string, true); } /** * This method splits a string without RegExes * * @param delim the delimiter * @param string the string to split * @param keepEmptyParts whether to keep empty parts * @return an array containing the parts */ public static String[] explode(String delim, String string, boolean keepEmptyParts) { int pos, offset = 0, delimLen = delim.length(); List<String> tokens = new ArrayList<>(); String part; while ((pos = string.indexOf(delim, offset)) > -1) { part = string.substring(offset, pos); if (part.length() > 0 || keepEmptyParts) { tokens.add(part); } offset = pos + delimLen; } part = string.substring(offset); if (part.length() > 0 || keepEmptyParts) { tokens.add(part); } return tokens.toArray(new String[tokens.size()]); } }