Java String Split split(String string)

Here you can find the source of split(String string)

Description

split

License

Open Source License

Declaration

public static String[] split(String string) 

Method Source Code


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

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

public class Main {
    public static String[] split(String string) {
        String s = string;/*from w  ww.j  a  va 2s  .  c om*/
        if (s != null)
            s = s.trim();
        String[] sArr = s.split("([ \t])+");
        List<String> list = new ArrayList<String>();
        boolean insideQuote = false;
        String tmp = null;
        for (int i = 0; i < sArr.length; i++) {
            if (!insideQuote && sArr[i].indexOf("\"") == -1)
                list.add(sArr[i]);
            else if (!insideQuote && sArr[i].startsWith("\"")) {
                insideQuote = true;
                tmp = sArr[i].substring(1);
                if (tmp.endsWith("\"")) {
                    insideQuote = false;
                    tmp = tmp.substring(0, tmp.length() - 1);
                    list.add(tmp);
                    tmp = null;
                }
            } else if (insideQuote && sArr[i].endsWith("\"")) {
                insideQuote = false;
                tmp += " " + sArr[i].substring(0, sArr[i].length() - 1);
                list.add(tmp);
                tmp = null;
            } else { //insideQuote && !sArr[i].endsWith("\"")
                tmp += " " + sArr[i];
            }
        }
        String[] retArr = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            retArr[i] = list.get(i);
        }
        return retArr;
    }
}

Related

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