public static Splitter with(final Pattern delimiter) throws IllegalArgumentException {
        N.checkArgNotNull(delimiter, "delimiter");
        N.checkArgument(!delimiter.matcher("").matches(), "Empty string may be matched by pattern: %s", delimiter);

        return new Splitter(new Strategy() {
            @Override
            public ObjIterator<String> split(final CharSequence source, final boolean omitEmptyStrings, final boolean trim, final boolean strip,
                    final int limit) {
                if (source == null) {
                    return ObjIterator.empty();
                }

                return new ObjIterator<String>() {
                    private final SubStringFunc subStringFunc = strip ? stripSubStringFunc : (trim ? trimSubStringFunc : defaultSubStringFunc);
                    private final int sourceLen = source.length();
                    private final Matcher matcher = delimiter.matcher(source);
                    private String next = null;
                    private int start = 0;
                    private int cursor = 0;
                    private int cnt = 0;
                    private boolean matches = false;

                    @Override
                    public boolean hasNext() {
                        if (next == null && (cursor >= 0 && cursor <= sourceLen)) {
                            if (limit - cnt == 1) {
                                next = subStringFunc.subString(source, start, sourceLen);
                                start = (cursor = sourceLen + 1);

                                if (omitEmptyStrings && next.length() == 0) {
                                    next = null;
                                }
                            } else {
                                while (cursor >= 0 && cursor <= sourceLen) {
                                    if (cursor == sourceLen || (matches = matcher.find(start))) {
                                        if (matches) {
                                            next = subStringFunc.subString(source, start, matcher.start());
                                            start = (cursor = matcher.end());
                                            matches = false;
                                        } else {
                                            next = subStringFunc.subString(source, start, sourceLen);
                                            start = (cursor = sourceLen + 1);
                                        }

                                        if (omitEmptyStrings && next.length() == 0) {
                                            next = null;
                                        }

                                        if (next != null) {
                                            break;
                                        }
                                    } else {
                                        cursor++;
                                    }
                                }
                            }
                        }

                        return next != null;
                    }

                    @Override
                    public String next() {
                        if (hasNext() == false) {
                            throw new NoSuchElementException();
                        }

                        final String result = next;
                        next = null;
                        cnt++;
                        return result;
                    }
                };
            }
        });
    }
public static Splitter with ( final Pattern delimiter ) throws IllegalArgumentException { N . checkArgNotNull ( delimiter , "delimiter" ) ; N . checkArgument ( ! delimiter . matcher ( "" ) . matches ( ) , "Empty string may be matched by pattern: %s" , delimiter ) ; return new Splitter ( new Strategy ( ) { @ Override public ObjIterator < String > split ( final CharSequence source , final boolean omitEmptyStrings , final boolean trim , final boolean strip , final int limit ) { if ( source == null ) { return ObjIterator . empty ( ) ; } return new ObjIterator < String > ( ) { private final SubStringFunc subStringFunc = strip ? stripSubStringFunc : ( trim ? trimSubStringFunc : defaultSubStringFunc ) ; private final int sourceLen = source . length ( ) ; private final Matcher matcher = delimiter . matcher ( source ) ; private String next = null ; private int start = 0 ; private int cursor = 0 ; private int cnt = 0 ; private boolean matches = false ; @ Override public boolean hasNext ( ) { if ( next == null && ( cursor >= 0 && cursor <= sourceLen ) ) { if ( limit - cnt == 1 ) { next = subStringFunc . subString ( source , start , sourceLen ) ; start = ( cursor = sourceLen + 1 ) ; if ( omitEmptyStrings && next . length ( ) == 0 ) { next = null ; } } else { while ( cursor >= 0 && cursor <= sourceLen ) { if ( cursor == sourceLen || ( matches = matcher . find ( start ) ) ) { if ( matches ) { next = subStringFunc . subString ( source , start , matcher . start ( ) ) ; start = ( cursor = matcher . end ( ) ) ; matches = false ; } else { next = subStringFunc . subString ( source , start , sourceLen ) ; start = ( cursor = sourceLen + 1 ) ; } if ( omitEmptyStrings && next . length ( ) == 0 ) { next = null ; } if ( next != null ) { break ; } } else { cursor ++ ; } } } } return next != null ; } @ Override public String next ( ) { if ( hasNext ( ) == false ) { throw new NoSuchElementException ( ) ; } final String result = next ; next = null ; cnt ++ ; return result ; } } ; } } ) ; }
@param delimiter
@return
@throws IllegalArgumentException if the specified {@code delimiter} is null, or empty string may be matched by it.