Java String Split by Separator split(final String iSource, final char iRecordSeparator, final char... iJumpCharacters)

Here you can find the source of split(final String iSource, final char iRecordSeparator, final char... iJumpCharacters)

Description

split

License

Apache License

Declaration

public static List<String> split(final String iSource,
            final char iRecordSeparator, final char... iJumpCharacters) 

Method Source Code

//package com.java2s;
/*//w w  w .  ja va  2s  .co  m
 * Copyright 1999-2010 Luca Garulli (l.garulli--at--orientechnologies.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.ArrayList;
import java.util.Collection;

import java.util.List;

public class Main {
    public static List<String> split(final String iSource,
            final char iRecordSeparator, final char... iJumpCharacters) {
        return split(iSource, 0, iSource.length(), iRecordSeparator,
                iJumpCharacters);
    }

    public static Collection<String> split(final Collection<String> iParts,
            final String iSource, final char iRecordSeparator,
            final char... iJumpCharacters) {
        return split(iParts, iSource, 0, iSource.length(),
                iRecordSeparator, iJumpCharacters);
    }

    public static List<String> split(final String iSource,
            final int iStartPosition, final int iEndPosition,
            final char iRecordSeparator, final char... iJumpCharacters) {
        return (List<String>) split(new ArrayList<String>(), iSource,
                iStartPosition, iSource.length(), iRecordSeparator,
                iJumpCharacters);
    }

    public static Collection<String> split(final Collection<String> iParts,
            final String iSource, final int iStartPosition,
            final int iEndPosition, final char iRecordSeparator,
            final char... iJumpCharacters) {
        final StringBuilder buffer = new StringBuilder();

        for (int i = iStartPosition; i < iEndPosition; ++i) {
            char c = iSource.charAt(i);

            if (c == iRecordSeparator) {
                iParts.add(buffer.toString());
                buffer.setLength(0);
            } else {
                if (iJumpCharacters.length > 0 && buffer.length() == 0) {
                    // CHECK IF IT'S A CHAR TO JUMP
                    if (!isCharPresent(c, iJumpCharacters)) {
                        buffer.append(c);
                    }
                } else
                    buffer.append(c);
            }
        }

        if (iJumpCharacters.length > 0 && buffer.length() > 0) {
            // CHECK THE END OF LAST ITEM IF NEED TO CUT THE CHARS TO JUMP
            char b;
            int newSize = 0;
            boolean found;
            for (int i = buffer.length() - 1; i >= 0; --i) {
                b = buffer.charAt(i);
                found = false;
                for (char j : iJumpCharacters) {
                    if (j == b) {
                        found = true;
                        ++newSize;
                        break;
                    }
                }
                if (!found)
                    break;
            }
            if (newSize > 0)
                buffer.setLength(buffer.length() - newSize);
        }

        iParts.add(buffer.toString());

        return iParts;
    }

    public static boolean isCharPresent(final char iCharacter,
            final char[] iCharacters) {
        final int len = iCharacters.length;
        for (int i = 0; i < len; ++i) {
            if (iCharacter == iCharacters[i]) {
                return true;
            }
        }

        return false;
    }
}

Related

  1. fastSplit(String text, char separator)
  2. split(byte[] b, int separator)
  3. split(CharSequence seq, char separator)
  4. split(final String expression, final char separator)
  5. split(final String input, final String separator)
  6. split(final String s, final char separator)
  7. split(final String str, final char separator)
  8. split(final String str, final char separator)
  9. split(final String str, final char separatorChar)