Java String Split split(String str, String token)

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

Description

split a String, if there are no character between two token this method place a empty String ( != String.split )

License

LGPL

Parameter

Parameter Description
str a standard str
token a token ( not pattern !!! )

Return

a array of String without the separator

Declaration

public static String[] split(String str, String token) 

Method Source Code

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

import java.util.ArrayList;

public class Main {
    /**//from   www .  j ava2 s  .c  o  m
      * split a String, if there are no character between two token this method place
      * a empty String ( != String.split )
      * 
      * @param str
      *            a standard str
      * @param token
      *            a token ( not pattern !!! )
      * @return a array of String without the separator
      */
    public static String[] split(String str, String token) {
        int i = str.indexOf(token);
        int start = 0;
        ArrayList<String> arrayList = new ArrayList<String>();
        while (i >= 0) {
            arrayList.add(str.substring(start, i));
            start = i + 1;
            i = str.indexOf(token, start);
        }
        arrayList.add(str.substring(start, str.length()));
        String[] res = new String[arrayList.size()];
        arrayList.toArray(res);
        return res;
    }
}

Related

  1. split(String str)
  2. split(String str, int ch)
  3. split(String str, String id)
  4. split(String str, String splitStr)
  5. split(String str, String splitter)
  6. split(String string)
  7. split(String string)
  8. split(String string, String seperator)
  9. split(String strSource, String strReg)