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

Parameter

Parameter Description
ch char.

Return

string array.

Declaration

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

Method Source Code

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

import java.util.*;

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

    /**//from  w w  w  . java 2s.  c om
     * split.
     * 
     * @param ch char.
     * @return string array.
     */
    public static String[] split(String str, char ch) {
        List<String> list = null;
        char c;
        int ix = 0, len = str.length();
        for (int i = 0; i < len; i++) {
            c = str.charAt(i);
            if (c == ch) {
                if (list == null)
                    list = new ArrayList<String>();
                list.add(str.substring(ix, i));
                ix = i + 1;
            }
        }
        if (ix > 0)
            list.add(str.substring(ix));
        return list == null ? EMPTY_STRING_ARRAY : (String[]) list.toArray(EMPTY_STRING_ARRAY);
    }
}

Related

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