Java String Split by Delimiter split(String src, String delimiter)

Here you can find the source of split(String src, String delimiter)

Description

split

License

Open Source License

Declaration

public static String[] split(String src, String delimiter) 

Method Source Code

//package com.java2s;

import java.util.ArrayList;

public class Main {

    public static String[] split(String src, String delimiter) {
        if (src == null || delimiter == null || src.trim().equals("") || delimiter.equals("")) {
            return new String[] { src };
        }/* w  ww . j  ava  2s .c  om*/

        ArrayList<String> list = new ArrayList<String>();

        int lengthOfDelimiter = delimiter.length();
        int pos = 0;
        while (true) {
            if (pos < src.length()) {

                int indexOfDelimiter = src.indexOf(delimiter, pos);
                if (indexOfDelimiter < 0) {
                    list.add(src.substring(pos));
                    break;
                } else {
                    list.add(src.substring(pos, indexOfDelimiter));
                    pos = indexOfDelimiter + lengthOfDelimiter;
                }
            } else if (pos == src.length()) {
                list.add("");
                break;
            } else {
                break;
            }
        }

        String[] result = new String[list.size()];
        list.toArray(result);
        return result;

    }

    public static boolean equals(String one, String another) {
        if (one == null) {
            if (another == null) {
                return true;
            } else {
                return false;
            }
        } else {
            if (another == null) {
                return false;
            } else {
                return one.equals(another);
            }
        }
    }

    public final static String trim(String target) {
        if (target == null)
            return null;

        target = target.trim();

        return "".equals(target) ? null : target;
    }

    public static String trim(Object src) {
        if (src == null) {
            return "";
        }

        String str = src.toString();
        return str.trim();
    }

    public static String subString(String parentStr, String startStr, String endStr) {
        return parentStr.substring(parentStr.indexOf(startStr) + startStr.length(), parentStr.indexOf(endStr));
    }
}

Related

  1. split(String s, String delimiter)
  2. split(String s, String delimiter)
  3. split(String s, String delimiter)
  4. split(String src, char delim)
  5. split(String src, char delim)
  6. split(String str, char delim)
  7. split(String str, char delimiter)
  8. split(String str, char delimiter)
  9. split(String str, char delimiter)