Java String Explode explode(String data, String delim)

Here you can find the source of explode(String data, String delim)

Description

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).

License

Open Source License

Parameter

Parameter Description
data the data to split
delim the delimiting string to use

Return

an array of strings, without the delimiter

Declaration

public static String[] explode(String data, String delim) 

Method Source Code


//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()]);
    }
}

Related

  1. explode(char separator, String[] input)
  2. explode(final String str, final char delimiter, final int limit)
  3. explode(int clr)
  4. explode(String csv)
  5. explode(String delim, String string)
  6. explode(String handleStr, String pointStr)
  7. explode(String input, final char delimiter, final char escape, final int capacity)
  8. explode(String s, String delim)