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

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

Description

split

License

Apache License

Declaration

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

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static final String[] EMPTY_STRING_ARRAY = new String[0];

    public static String[] split(final String str, final char separatorChar) {
        if (str == null) {
            return null;
        }//ww w . j a  va2  s . c om
        final int len = str.length();
        if (len == 0) {
            return EMPTY_STRING_ARRAY;
        }
        final List<String> list = new ArrayList<>();
        int i = 0, start = 0;
        boolean match = false;
        while (i < len) {
            if (str.charAt(i) == separatorChar) {
                if (match) {
                    list.add(str.substring(start, i));
                    match = false;
                }
                start = ++i;
            } else {
                match = true;
                i++;
            }
        }
        if (match) {
            list.add(str.substring(start, i));
        } else if (list.isEmpty()) {
            return EMPTY_STRING_ARRAY;
        }
        return list.toArray(new String[list.size()]);
    }

    public static boolean isEmpty(String str) {
        return str == null || str.isEmpty();
    }
}

Related

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