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

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

Description

split

License

Apache License

Declaration

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

Method Source Code


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

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

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

    public static String[] split(String str, char separatorChar, boolean preserveAllTokens) {
        // Performance tuned for 2.0 (JDK1.4)

        if (str == null) {
            return null;
        }// w  w  w.ja v a2s .  c o m
        int len = str.length();
        if (len == 0) {
            return EMPTY_STRING_ARRAY;
        }
        List<String> list = new ArrayList<String>();
        int i = 0, start = 0;
        boolean match = false;
        boolean lastMatch = false;
        while (i < len) {
            if (str.charAt(i) == separatorChar) {
                if (match || preserveAllTokens) {
                    list.add(str.substring(start, i));
                    match = false;
                    lastMatch = true;
                }
                start = ++i;
                continue;
            }
            lastMatch = false;
            match = true;
            i++;
        }
        if (match || (preserveAllTokens && lastMatch)) {
            list.add(str.substring(start, i));
        }
        return (String[]) list.toArray(new String[list.size()]);
    }
}

Related

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