Java String Split split(String s)

Here you can find the source of split(String s)

Description

Split input into substrings on the assumption that it is either only one string or it was generated using List.toString(), with tokens
SPLIT_START {string} { SPLIT_DELIM {string}} SPLIT_END
 (e.g., "[one, two, three]"). 

License

Open Source License

Declaration

public static String[] split(String s) 

Method Source Code

//package com.java2s;
/* *******************************************************************
 * Copyright (c) 1999-2000 Xerox Corporation. 
 * All rights reserved. //from   w  w  w .ja va 2 s .c  o m
 * 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: 
 *     Xerox/PARC     initial implementation 
 * ******************************************************************/

import java.util.ArrayList;

public class Main {
    /** Delimiter used by split(String) (and ArrayList.toString()?) */
    public static final String SPLIT_DELIM = ", ";
    /** prefix used by split(String) (and ArrayList.toString()?) */
    public static final String SPLIT_START = "[";
    /** suffix used by split(String) (and ArrayList.toString()?) */
    public static final String SPLIT_END = "]";

    /**
     * Split input into substrings on the assumption that it is
     * either only one string or it was generated using List.toString(),
     * with tokens
     * <pre>SPLIT_START {string} { SPLIT_DELIM {string}} SPLIT_END<pre>
     * (e.g., <code>"[one, two, three]"</code>).
     */
    public static String[] split(String s) {
        if (null == s) {
            return null;
        }
        if ((!s.startsWith(SPLIT_START)) || (!s.endsWith(SPLIT_END))) {
            return new String[] { s };
        }
        s = s.substring(SPLIT_START.length(), s.length() - SPLIT_END.length());
        final int LEN = s.length();
        int start = 0;
        final ArrayList result = new ArrayList();
        final String DELIM = ", ";
        int loc = s.indexOf(SPLIT_DELIM, start);
        while ((start < LEN) && (-1 != loc)) {
            result.add(s.substring(start, loc));
            start = DELIM.length() + loc;
            loc = s.indexOf(SPLIT_DELIM, start);
        }
        result.add(s.substring(start));
        return (String[]) result.toArray(new String[0]);
    }
}

Related

  1. split(String input)
  2. split(String input)
  3. split(String s)
  4. split(String s)
  5. split(String s)
  6. split(String s)
  7. split(String s, int c)
  8. split(String s, String at)
  9. split(String s, String s1)