Java String Split split(String self)

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

Description

split

License

Open Source License

Declaration

public static List split(String self) 

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 List split(String self) {
        return split(self, null, true);
    }/*from www.  j a  va  2  s . co m*/

    public static List split(String self, Long separator) {
        return split(self, separator, true);
    }

    public static List split(String self, Long separator, boolean trimmed) {
        if (separator == null)
            return splitws(self);
        int sep = separator.intValue();
        boolean trim = trimmed;
        List toks = new ArrayList<String>(16);
        int len = self.length();
        int x = 0;
        for (int i = 0; i < len; ++i) {
            if (self.charAt(i) != sep)
                continue;
            if (x <= i)
                toks.add(splitStr(self, x, i, trim));
            x = i + 1;
        }
        if (x <= len)
            toks.add(splitStr(self, x, len, trim));
        return toks;
    }

    public static List splitws(String val) {
        List toks = new ArrayList<String>(16);
        int len = val.length();
        while (len > 0 && val.charAt(len - 1) <= ' ')
            --len;
        int x = 0;
        while (x < len && val.charAt(x) <= ' ')
            ++x;
        for (int i = x; i < len; ++i) {
            if (val.charAt(i) > ' ')
                continue;
            toks.add(val.substring(x, i));
            x = i + 1;
            while (x < len && val.charAt(x) <= ' ')
                ++x;
            i = x;
        }
        if (x <= len)
            toks.add(val.substring(x, len));
        if (toks.size() == 0)
            toks.add("");
        return toks;
    }

    private static String splitStr(String val, int s, int e, boolean trim) {
        if (trim) {
            while (s < e && val.charAt(s) <= ' ')
                ++s;
            while (e > s && val.charAt(e - 1) <= ' ')
                --e;
        }
        return val.substring(s, e);
    }
}

Related

  1. split(String s, String s1)
  2. split(String s, String seperator)
  3. split(String s, String spliter, boolean removeEmptyItem)
  4. split(String s, String splitStr)
  5. split(String s, String splitter)
  6. split(String signature, boolean skipInitialAngleBracket)
  7. split(String source, String seperator)
  8. split(String str)
  9. split(String str)