Java String Split by Char split(String cs, char sep)

Here you can find the source of split(String cs, char sep)

Description

Splits a String around occurences of a character.

License

LGPL

Declaration

public static List<String> split(String cs, char sep) 

Method Source Code


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

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

public class Main {
    /**//from  w w w . ja  v  a  2  s.com
     * Splits a String around occurences of a character. The result is similar to
     * {@link String#split(String)}.
     */
    public static List<String> split(String cs, char sep) {
        List<String> list = new ArrayList<>(4);
        split(cs, sep, list);
        return list;
    }

    /**
     * Splits a String around occurences of a character, and put the result in a List. The result is similar
     * to {@link String#split(String)}.
     */
    public static void split(String str, char sep, List<String> list) {
        int pos0 = 0;
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (ch == sep) {
                list.add(str.substring(pos0, i));
                pos0 = i + 1;
            }
        }
        if (pos0 < str.length()) {
            list.add(str.substring(pos0, str.length()));
        }
    }
}

Related

  1. split(char c, String s)
  2. split(char elem, String orig)
  3. split(char sep, String input)
  4. split(final String input, final char split)
  5. split(final String string, final char... toSplit)
  6. split(String s, char c)
  7. split(String s, char c)
  8. split(String s, char c)
  9. split(String s, char c, int limit)