Java String Explode explode(String delim, String string)

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

Description

This method splits a string without RegExes

License

Open Source License

Parameter

Parameter Description
delim the delimiter
string the string to split

Return

an array containing the parts

Declaration

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

Method Source Code

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

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 data, String delim)
  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)
  9. explode(String source, String deliminator)