Here you can find the source of explode(String data, String delim)
Parameter | Description |
---|---|
data | the data to split |
delim | the delimiting string to use |
public static String[] explode(String data, String delim)
//package com.java2s; /*//from w w w. j av a 2 s . co m * The MIT License * * Copyright (c) 2012 David Morgan, University of Rochester Medical Center * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ import java.util.ArrayList; public class Main { /** * Operates a lot like the split function on String, but does not require the delimiter to be * a regular expression (this does not matter much, except when dynamically loading the delim * that may have regular expression special chars in it). * @param data the data to split * @param delim the delimiting string to use * @return an array of strings, without the delimiter */ public static String[] explode(String data, String delim) { ArrayList<String> list = new ArrayList<String>(); StringBuffer sb = new StringBuffer(data); boolean splitting = true; int curpos = 0; int delimpos = sb.indexOf(delim); while (splitting) { if (delimpos >= 0) { list.add(sb.substring(curpos, delimpos)); } else { list.add(sb.substring(curpos)); splitting = false; } curpos = delimpos + delim.length(); delimpos = sb.indexOf(delim, curpos); } return list.toArray(new String[list.size()]); } }