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

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

Description

split

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;

import java.util.*;

public class Main {
    public static final String[] EMPTY_STRINGS = {};

    public static String[] split(String str, char separatorChar) {
        if (str == null) {
            return null;
        }//ww w  .j  a v  a 2 s  .c  o m
        int length = str.length();

        if (length == 0) {
            return EMPTY_STRINGS;
        }

        List<String> list = new ArrayList();
        int i = 0;
        int start = 0;
        boolean match = false;

        while (i < length) {
            if (str.charAt(i) == separatorChar) {
                if (match) {
                    list.add(str.substring(start, i));
                    match = false;
                }
                start = ++i;
                continue;
            }
            match = true;
            i++;
        }
        if (match) {
            list.add(str.substring(start, i));
        }
        return list.toArray(new String[list.size()]);
    }
}

Related

  1. split(String str, char separator)
  2. split(String str, char separator)
  3. split(String str, char separator)
  4. split(String str, char separator)
  5. split(String str, char separatorChar)
  6. split(String str, char separatorChar)
  7. split(String str, char separatorChar)
  8. split(String str, char separatorChar)
  9. split(String str, char separatorChar, boolean preserveAllTokens)