Java String Split by Delimiter split(String string, String delimiter)

Here you can find the source of split(String string, String delimiter)

Description

Splits the given string by delimiter.

License

Open Source License

Parameter

Parameter Description
string a parameter
delimiter a parameter

Return

the unprocessed array

Declaration

public static String[] split(String string, String delimiter) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *  Copyright (c) 2010 Weltevree Beheer BV, Remain Software & Industrial-TSI
 *                                                                      
 * All rights reserved. This program and the accompanying materials     
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at             
 * http://www.eclipse.org/legal/epl-v10.html                            
 *                                                                      
 * Contributors:                                                        
 *    Wim Jongman - initial API and implementation
 *******************************************************************************/

import java.util.ArrayList;

public class Main {
    /**//w  w  w . j  av a  2s.c  o  m
     * Splits the given string by delimiter.
     * 
     * @param string
     * @param delimiter
     * @return the unprocessed array
     */
    public static String[] split(String string, String delimiter) {

        String[] result;

        if (string == null) {
            result = new String[0];
            return result;
        }

        ArrayList list = new ArrayList();

        do {
            int location = string.indexOf(delimiter);

            if (location < 0)
                break;
            list.add(string.substring(0, location).trim());
            string = string.substring(location + 1);
        } while (true);

        string = string.trim();

        if (string.length() > 0)
            list.add(string);

        result = (String[]) list.toArray(new String[list.size()]);

        // StringTokenizer tizer = new StringTokenizer(string, delimiter);
        // String[] result = new String[tizer.countTokens()];
        // for (int i = 0; tizer.hasMoreElements(); i++) {
        // result[i] = tizer.nextToken();
        // }
        return result;

    }
}

Related

  1. split(String str, String delimiter)
  2. split(String str, String delimiter, int limit)
  3. split(String str, String delims)
  4. split(String str, String regexDelim, int limitUseRegex)
  5. split(String string, String delim, boolean doTrim)
  6. split(String string, String delimiter)
  7. split(String stringToSplit, char delimiter)
  8. split(String strToSplit, char delimiter)
  9. split(String text, String delimiter)