Java Array Split split(String string, String delim, String[] a)

Here you can find the source of split(String string, String delim, String[] a)

Description

split

License

Apache License

Parameter

Parameter Description
string a parameter
delim a parameter
a a parameter

Exception

Parameter Description
IllegalArgumentException if a is of length 0
ArrayIndexOutOfBoundsException if a is too small to fit all strings

Return

number of string written to a

Declaration

public static int split(String string, String delim, String[] a) 

Method Source Code

//package com.java2s;
/**/*from   www  .  java 2  s .c  o  m*/
 * Copyright 2010 Molindo GmbH
 *
 * 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.Iterator;

import java.util.NoSuchElementException;

public class Main {
    /**
     *
     * @param string
     * @param delim
     * @param a
     * @return number of string written to a
     * @throws IllegalArgumentException
     *             if a is of length 0
     * @throws ArrayIndexOutOfBoundsException
     *             if a is too small to fit all strings
     * @see #split(String, String)
     */
    public static int split(String string, String delim, String[] a) {
        if (a.length == 0) {
            throw new IllegalArgumentException();
        }

        int idx = 0;
        for (String s : split(string, delim)) {
            a[idx++] = s;
        }
        return idx;
    }

    public static Iterable<String> split(final String string, final String split) {
        return split(string, split, Integer.MAX_VALUE);
    }

    public static Iterable<String> split(final String string, final String split, final int max) {
        if (string == null) {
            throw new NullPointerException("string");
        }
        if (split == null) {
            throw new NullPointerException("split");
        }
        if (split.isEmpty()) {
            throw new IllegalArgumentException("split must not be empty");
        }
        if (max <= 0) {
            throw new IllegalArgumentException("max must be >= 1, was " + max);
        }

        return new Iterable<String>() {

            @Override
            public Iterator<String> iterator() {
                return new Iterator<String>() {

                    private int _count = 0;
                    private int _pos = 0;

                    @Override
                    public boolean hasNext() {
                        return _pos >= 0;
                    }

                    @Override
                    public String next() {
                        if (!hasNext()) {
                            throw new NoSuchElementException();
                        }

                        int next = ++_count == max ? -1 : string.indexOf(split, _pos);

                        String str;

                        if (next > 0) {
                            str = string.substring(_pos, next);
                            _pos = next + split.length();
                        } else {
                            str = string.substring(_pos);
                            _pos = -1;
                        }

                        return str;
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException();
                    }

                };
            }
        };
    }

    public static int length(String string) {
        return string == null ? 0 : string.length();
    }

    public static int length(String... strings) {
        int length = 0;
        for (int i = 0; i < strings.length; i++) {
            length += length(strings[i]);
        }
        return length;
    }
}

Related

  1. split(byte[] pattern, byte[] src)
  2. split(byte[] source, int c)
  3. split(Collection collection, C[] array, int pageSize)
  4. split(final int[] array)
  5. split(int splitBefore, byte[] source)
  6. splitAndPad(byte[] byteArray, int blocksize)
  7. splitArray(byte[] src, int size)
  8. splitArray(int[] array, int limit)
  9. splitArray(T[] array, int capacity)