Java String Explode explode(String source, String deliminator)

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

Description

Performs an 'explode' operation on the given source string.

License

Open Source License

Declaration

public static List explode(String source, String deliminator) 

Method Source Code

//package com.java2s;
/**//from  www. j  a v a 2s  .c om
 * com.mckoi.util.StringUtil  17 Dec 1999
 *
 * Mckoi SQL Database ( http://www.mckoi.com/database )
 * Copyright (C) 2000, 2001, 2002  Diehl and Associates, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * Version 2 as published by the Free Software Foundation.
 *
 * 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 Version 2 for more details.
 *
 * You should have received a copy of the GNU General Public License
 * Version 2 along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Change Log:
 * 
 * 
 */

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Performs an 'explode' operation on the given source string.  This
     * algorithm finds all instances of the deliminator string, and returns an
     * array of sub-strings of between the deliminator.  For example,
     * <code>
     *   explode("10:30:40:55", ":") = ({"10", "30", "40", "55"})
     * </code>
     */
    public static List explode(String source, String deliminator) {
        ArrayList list = new ArrayList();
        int i = find(source, deliminator);
        while (i != -1) {
            list.add(source.substring(0, i));
            source = source.substring(i + deliminator.length());
            i = find(source, deliminator);
        }
        list.add(source);
        return list;
    }

    /**
     * Finds the index of the given string in the source string.
     * <p>
     * @return -1 if the 'find' string could not be found.
     */
    public static int find(String source, String find) {
        return source.indexOf(find);

        //    int find_index = 0;
        //    int len = source.length();
        //    int find_len = find.length();
        //    int i = 0;
        //    for (; i < len; ++i) {
        //      if (find_index == find_len) {
        //        return i - find_len;
        //      }
        //      if (find.indexOf(source.charAt(i), find_index) == find_index) {
        //        ++find_index;
        //      }
        //      else {
        //        find_index = 0;
        //      }
        //    }
        //    if (find_index == find_len) {
        //      return i - find_len;
        //    }
        //    else {
        //      return -1;
        //    }
    }
}

Related

  1. explode(String data, String delim)
  2. explode(String delim, String string)
  3. explode(String handleStr, String pointStr)
  4. explode(String input, final char delimiter, final char escape, final int capacity)
  5. explode(String s, String delim)
  6. explode(String split, String input)
  7. explode(String src, String sep)
  8. explode(String string, String separators)
  9. explode(String tokenizer, String text)