Java String Split split(String token, String s)

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

Description

split

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
/*/*  w w  w . j  av a 2  s .  c o  m*/
 * StringUtil.java
 *
 * Copyright (c) 2005-2009 Andrew Krizhanovsky <andrew.krizhanovsky at gmail.com>
 * Distributed under GNU Public License.
 */

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

public class Main {
    private final static String[] NULL_STRING_ARRAY = new String[0];

    public static String[] split(String token, String s) {
        if (null == s || 0 == s.length())
            return NULL_STRING_ARRAY;

        List<String> ls = new ArrayList<String>();

        int previousLoc = 0;
        int loc = s.indexOf(token, previousLoc);
        if (-1 == loc) {
            ls.add(s);
        } else {
            do {
                ls.add(s.substring(previousLoc, loc));
                previousLoc = (loc + token.length());
                loc = s.indexOf(token, previousLoc);
            } while ((loc != -1) && (previousLoc < s.length()));

            ls.add(s.substring(previousLoc));
        }

        return ((String[]) ls.toArray(NULL_STRING_ARRAY));
    }
}

Related

  1. split(String strSource, String strReg)
  2. split(String text)
  3. split(String text)
  4. split(String text)
  5. split(String text, String token)
  6. split(String toSplit)
  7. split(String value)
  8. split(String value)
  9. split_fields(String str)