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

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

Description

Splits a string into substrings.

License

LGPL

Parameter

Parameter Description
str The string which is to split.
delim The delimiter character, for example a space <code>' '</code>.
trailing The ending which is added as a substring though it wasn't in the <code>str</code>. This parameter is just for the <code>IRCParser</code> class which uses this method to split the <code>middle</code> part into the parameters. But as last parameter always the <code>trailing</code> is added. This is done here because it's the fastest way to do it here. If the <code>end</code> is <code>null</code> or <code>""</code>, nothing is appended.

Return

An array with all substrings.

Declaration

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

Method Source Code

//package com.java2s;
/**/*w  w  w.jav a2  s.c o  m*/
 * IRClib - A Java Internet Relay Chat library
 * Copyright (C) 2006-2015 Christoph Schwering <schwering@gmail.com>
 * and/or other contributors as indicated by the @author tags.
 *
 * This library and the accompanying materials are made available under the
 * terms of the
 *  - GNU Lesser General Public License,
 *  - Apache License, Version 2.0 and
 *  - Eclipse Public License v1.0.
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY.
 */

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class Main {
    /**
     * Splits a string into substrings.
     * @param str The string which is to split.
     * @param delim The delimiter character, for example a space <code>' '</code>.
     * @param trailing The ending which is added as a substring though it wasn't
     *                 in the <code>str</code>. This parameter is just for the
     *                 <code>IRCParser</code> class which uses this method to
     *                 split the <code>middle</code> part into the parameters.
     *                 But as last parameter always the <code>trailing</code> is
     *                 added. This is done here because it's the fastest way to
     *                 do it here.
     *                 If the <code>end</code> is <code>null</code> or
     *                 <code>""</code>, nothing is appended.
     * @return An array with all substrings.
     * @see #split(String, int)
     */
    public static String[] split(String str, int delim, String trailing) {
        List<String> items = new ArrayList<String>(15);
        int last = 0;
        int index = 0;
        int len = str.length();
        while (index < len) {
            if (str.charAt(index) == delim) {
                items.add(str.substring(last, index));
                last = index + 1;
            }
            index++;
        }
        if (last != len)
            items.add(str.substring(last));
        if (trailing != null && trailing.length() != 0)
            items.add(trailing);
        String[] result = items.toArray(new String[0]);
        return result;
    }

    /**
     * Splits a string into substrings. This method is totally equal to
     * <code>split(str, delim, null)</code>.
     * @param str The string which is to split.
     * @param delim The delimiter character, for example a space <code>' '</code>.
     * @return An array with all substrings.
     * @see #split(String, int, String)
     */
    public static String[] split(String str, int delim) {
        return split(str, delim, null);
    }

    public static int[] toArray(Collection<Integer> list) {
        if (list == null || list.isEmpty()) {
            return new int[0];
        }
        int[] result = new int[list.size()];
        int i = 0;
        for (int value : list) {
            result[i++] = value;
        }
        return result;
    }
}

Related

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