Java String Split by Char split(String str, char ch)

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

Description

split

License

Apache License

Declaration

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

Method Source Code

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

import java.util.ArrayList;

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

    public static String[] split(String str, char ch) {
        ArrayList list = null;//from w w w.jav  a  2 s .  co  m
        int ix = 0;
        int len = str.length();

        for (int i = 0; i < len; ++i) {
            char c = str.charAt(i);
            if (c == ch) {
                if (list == null) {
                    list = new ArrayList();
                }

                list.add(str.substring(ix, i));
                ix = i + 1;
            }
        }

        if (ix > 0) {
            list.add(str.substring(ix));
        }

        return list == null ? EMPTY_STRING_ARRAY
                : (String[]) ((String[]) list.toArray(EMPTY_STRING_ARRAY));
    }
}

Related

  1. split(String s, char sep)
  2. split(String splittee, String splitChar)
  3. split(String str, char c)
  4. split(String str, char c)
  5. split(String str, char ch)
  6. split(String str, char ch, int size)
  7. split(String str, char cha)
  8. split(String str, char deli)
  9. split(String str, char sep)