Java String Split split(final String string)

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

Description

split

License

Apache License

Declaration

public static String[] split(final String string) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w  .j ava  2  s .  co  m*/
 * =============================================================================
 * 
 *   Copyright (c) 2007-2010, The JASYPT team (http://www.jasypt.org)
 * 
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 * =============================================================================
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static String[] split(final String string) {
        // Whitespace will be used as separator
        return split(string, null);
    }

    public static String[] split(final String string,
            final String separators) {

        if (string == null) {
            return null;
        }

        final int length = string.length();

        if (length == 0) {
            return new String[0];
        }

        final List results = new ArrayList();
        int i = 0;
        int start = 0;
        boolean tokenInProgress = false;

        if (separators == null) {

            while (i < length) {
                if (Character.isWhitespace(string.charAt(i))) {
                    if (tokenInProgress) {
                        results.add(string.substring(start, i));
                        tokenInProgress = false;
                    }
                    start = ++i;
                    continue;
                }
                tokenInProgress = true;
                i++;
            }

        } else if (separators.length() == 1) {

            final char separator = separators.charAt(0);
            while (i < length) {
                if (string.charAt(i) == separator) {
                    if (tokenInProgress) {
                        results.add(string.substring(start, i));
                        tokenInProgress = false;
                    }
                    start = ++i;
                    continue;
                }
                tokenInProgress = true;
                i++;
            }

        } else {

            while (i < length) {
                if (separators.indexOf(string.charAt(i)) >= 0) {
                    if (tokenInProgress) {
                        results.add(string.substring(start, i));
                        tokenInProgress = false;
                    }
                    start = ++i;
                    continue;
                }
                tokenInProgress = true;
                i++;
            }

        }

        if (tokenInProgress) {
            results.add(string.substring(start, i));
        }

        return (String[]) results.toArray(new String[results.size()]);

    }
}

Related

  1. split(final String s)
  2. split(final String s)
  3. split(final String str)
  4. split(final String str)
  5. split(final String string)
  6. split(final String string, final String toSplit)
  7. split(final String text, final String sp)
  8. split(final String toSplit)
  9. split(Iterable objects, boolean even)