Java String Split by Separator split(String s, String separator)

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

Description

split

License

Apache License

Declaration

public static List<String> split(String s, String separator) 

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(String s, String separator) {
        List<String> result = new ArrayList<>();
        int lastIndex = 0;
        int index = s.indexOf(separator);

        while (index != -1) {
            String substring = s.substring(lastIndex, index);
            if (!substring.equals(separator) && index > 0)
                result.add(substring);/*from w  w  w. j  a  va  2  s  .co m*/

            lastIndex = index + separator.length();
            index = s.indexOf(separator, lastIndex);
        }
        if (lastIndex < s.length())
            result.add(s.substring(lastIndex, s.length()));

        return result;
    }
}

Related

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