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 List<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 List<String> split(final String str,
            final char separatorChar) {
        return split(str, separatorChar, 10);
    }/*w ww  . jav a  2 s.  co  m*/

    public static List<String> split(final String str,
            final char separatorChar, int expectParts) {
        if (str == null) {
            return null;
        }

        final int len = str.length();
        if (len == 0) {
            return new ArrayList<>();
        }
        final List<String> list = new ArrayList<String>(expectParts);
        int i = 0;
        int 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;
                continue;
            }
            match = true;
            i++;
        }
        if (match) {
            list.add(str.substring(start, i));
        }
        return list;
    }
}

Related

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