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

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

Description

Splits "string" by "Delimiter"

License

Open Source License

Parameter

Parameter Description
str - the string to be split
delimiter - the field delimiter within str

Declaration

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

Method Source Code

//package com.java2s;
/*/*w ww  .  j av a  2s  .  c o  m*/
 This file is part of the BlueJ program. 
 Copyright (C) 1999-2009,2011,2012,2013,2014,2015  Michael Kolling and John Rosenberg 
    
 This program 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 2 
 of the License, or (at your option) any later version. 
    
 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 for more details. 
    
 You should have received a copy of the GNU General Public License 
 along with this program; if not, write to the Free Software 
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. 
    
 This file is subject to the Classpath exception as provided in the  
 LICENSE.txt file that accompanied this code.
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Splits "string" by "Delimiter"
     * 
     * @param str - the string to be split
     * @param delimiter - the field delimiter within str
     * @returns an array of Strings
     */
    public static String[] split(String str, String delimiter) {
        List<String> strings = new ArrayList<String>();
        int start = 0;
        int len = str.length();
        int dlen = delimiter.length();
        int offset = str.lastIndexOf(delimiter); // First of all, find the
        // Last occurance of the Delimiter
        // Stop empty delimiters
        if (dlen < 1)
            return null;
        else if (offset < 0) // one element
        {
            String[] result = { str };
            return result;
        }

        //
        // Append the delimiter onto the end if it doesn't already exit
        //
        if (len > offset + dlen) {
            str += delimiter;
            len += dlen;
        }

        do {
            // Get the new Offset
            offset = str.indexOf(delimiter, start);
            strings.add(str.substring(start, offset));

            // Get the new Start position
            start = offset + dlen;
        } while ((start < len) && (offset != -1));

        // Convert the list into an Array of Strings
        String result[] = new String[strings.size()];
        strings.toArray(result);
        return result;
    }
}

Related

  1. split(String str, String delim)
  2. split(String str, String delim)
  3. split(String str, String delim)
  4. split(String str, String delim)
  5. split(String str, String delimeter)
  6. split(String str, String delimiter)
  7. split(String str, String delimiter)
  8. split(String str, String delimiter)
  9. split(String str, String delimiter)