Java String Split by Separator split(final String str, final char separator)

Here you can find the source of split(final String str, final char separator)

Description

Split string by special separator char.

License

Apache License

Parameter

Parameter Description
str the String to parse
separator the character used as the delimiter

Return

an array of parsed Strings

Declaration

public static String[] split(final String str, final char separator) 

Method Source Code


//package com.java2s;
/*/*from   ww w  .  ja v  a2s.  c  o m*/
 *     Copyright 2016-2026 TinyZ
 *
 * 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 {
    /**
     * Split string by special separator char.
     * <pre>
     *     split(",ab,cd,ef,gh", ',') => ["", "ab", "cd", "ef", "gh"]
     *     split(",ab,cd,ef,gh", '|') => [",ab,cd,ef,gh"]
     * </pre>
     *
     * @param str       the String to parse
     * @param separator the character used as the delimiter
     * @return an array of parsed Strings
     */
    public static String[] split(final String str, final char separator) {
        return split(str, separator, 0);
    }

    /**
     * Split string by special separator char.
     * <pre>
     *     split(",ab,cd,ef,gh", ',', 0) => ["", "ab", "cd", "ef", "gh"]
     *     split(",ab,cd,ef,gh", ',', 2) => ["", "ab,cd,ef,gh"]
     *     split("ab,cd,ef,gh", ',', 2) => ["ab", "cd,ef,gh"]
     *     split("ab,cd,ef,gh", ',', 3) => ["ab", "cd", "ef,gh"]
     * </pre>
     *
     * @param str       the String to parse
     * @param separator the character used as the delimiter
     * @param max       the maximum number of elements to include in the array. A zero or negative value implies no limit
     * @return an array of parsed Strings
     */
    public static String[] split(final String str, final char separator, final int max) {
        if (isEmpty(str)) {
            return null;
        }
        final List<String> list = new ArrayList<>();
        int start = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == separator) {
                list.add(str.substring(start, i));
                if (max > 1 && list.size() >= max - 1) {
                    break;
                }
                start = i + 1;
            }
        }
        if (start < str.length()) {
            list.add(str.substring(start, str.length()));
        }
        return list.toArray(new String[list.size()]);
    }

    /**
     * Split string by special separator char.
     * <pre>
     *     split(",ab,cd,ef,gh", ',', 0, true) => ["", "ab", "cd", "ef", "gh"]
     *     split(",ab,cd,ef,gh", ',', 0, false) => ["ab", "cd", "ef", "gh"]
     *     split(",ab,cd,ef,gh", ',', 2, true) => ["", "ab,cd,ef,gh"]
     *     split("ab,cd,ef,gh", ',', 2, true) => ["ab", "cd,ef,gh"]
     *     split("ab,cd,ef,gh", ',', 3, true) => ["ab", "cd", "ef,gh"]
     * </pre>
     *
     * @param str        the String to parse
     * @param separator  the character used as the delimiter
     * @param max        the maximum number of elements to include in the array. A zero or negative value implies no limit
     * @param allowEmpty is allow element is empty("").
     * @return an array of parsed Strings
     */
    public static String[] split(final String str, final char separator, final int max, final boolean allowEmpty) {
        if (isEmpty(str)) {
            return null;
        }
        final List<String> list = new ArrayList<>();
        int start = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == separator) {
                if (!allowEmpty && i - start <= 0) {
                    start = i + 1;
                    continue;
                }
                list.add(str.substring(start, i));
                start = i + 1;
                if (max > 1 && list.size() >= max - 1) {
                    break;
                }
            }
        }
        if (start < str.length()) {
            list.add(str.substring(start, str.length()));
        }
        return list.toArray(new String[list.size()]);
    }

    /**
     * Check if a CharSequence is null or empty (equals "").
     *
     * @param cs The checked {@link CharSequence}
     * @return Return true if the string is not null and !.equals("").
     */
    public static boolean isEmpty(CharSequence cs) {
        return null == cs || cs.length() == 0;
    }
}

Related

  1. split(CharSequence seq, char separator)
  2. split(final String expression, final char separator)
  3. split(final String input, final String separator)
  4. split(final String iSource, final char iRecordSeparator, final char... iJumpCharacters)
  5. split(final String s, final char separator)
  6. split(final String str, final char separator)
  7. split(final String str, final char separatorChar)
  8. split(final String str, final char separatorChar)
  9. split(final String str, final char[] separators)