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

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

Description

Splits a string containing n occurrences of delim into n+1 strings that don't contain delim (and whose occurrences in str are bounded by delim).

License

Open Source License

Parameter

Parameter Description
str a <code>String</code> value
delim a <code>char</code> value

Return

a String[] value

Declaration

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

Method Source Code


//package com.java2s;
/* J_LZ_COPYRIGHT_BEGIN *******************************************************
* Copyright 2001-2006, 2011 Laszlo Systems, Inc.  All Rights Reserved.        *
* Use is subject to license terms.                                            *
* J_LZ_COPYRIGHT_END *********************************************************/

import java.util.*;

public class Main {
    /**/*from w w  w  .j  ava  2 s.co  m*/
     * Splits a string containing <var>n</var> occurrences of
     * <var>delim</var> into <var>n+1</var> strings that don't contain
     * <var>delim</var> (and whose occurrences in <var>str</var> are
     * bounded by <var>delim</var>).
     *
     * <p>For example, <code>split("a,b,,c", ",")<code> evaluates to
     * <code>{"a", "b", "", "c"}</code>.
        
     * <p><code>split("/", "/") -> ["", ""]</code>
     *
     * @param str a <code>String</code> value
     * @param delim a <code>char</code> value
     * @return a <code>String[]</code> value
     */
    public static String[] split(String str, String delim) {
        List<String> lines = new ArrayList<String>();
        int startPos = 0;
        while (true) {
            int endPos = indexOf(str, delim, startPos);
            if (endPos == -1) {
                if (startPos > 0 || startPos < str.length()) {
                    lines.add(str.substring(startPos));
                }
                break;
            }
            lines.add(str.substring(startPos, endPos));
            startPos = endPos + delim.length();
        }
        {
            String[] result = new String[lines.size()];
            int i = 0;
            for (String line : lines) {
                result[i] = line;
                i++;
            }
            return result;
        }
    }

    /** Return the index of the first occurrence of value in str that
     * is later than or equal to offset.
     * @param str a String
     * @param value a String
     * @param offset a String
     * @return an int
     */
    public static int indexOf(String str, String value, int offset) {
        if (value.length() == 0) {
            throw new IllegalArgumentException();
        }
        while (offset < str.length()) {
            int pos = str.indexOf(value.charAt(0), offset);
            if (pos == -1) {
                return pos;
            }
            for (int i = 1;; i++) {
                if (i >= value.length()) {
                    return pos;
                }
                if (pos + i >= str.length() || str.charAt(pos + i) != value.charAt(i)) {
                    break;
                }
            }
            offset = pos + 1;
        }
        return -1;
    }
}

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 delim)
  6. split(String str, String delimeter)
  7. split(String str, String delimiter)
  8. split(String str, String delimiter)
  9. split(String str, String delimiter)