Java String Split by Separator split(String str, char separator)

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

Description

split

License

Open Source License

Declaration

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

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

public class Main {
    public static String[] split(String str, char separator) {
        // String.split returns a single empty result for splitting the empty
        // string.
        if (str.isEmpty()) {
            return new String[] { "" };
        }// w  w w  .jav a 2 s . co m
        ArrayList<String> strList = new ArrayList<>();
        int startIndex = 0;
        int nextIndex = 0;
        while ((nextIndex = str.indexOf(separator, startIndex)) != -1) {
            strList.add(str.substring(startIndex, nextIndex));
            startIndex = nextIndex + 1;
        }
        strList.add(str.substring(startIndex));
        // remove trailing empty split(s)
        int last = strList.size(); // last split
        while (--last >= 0 && "".equals(strList.get(last))) {
            strList.remove(last);
        }
        return strList.toArray(new String[strList.size()]);
    }
}

Related

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