Java String Split by Separator splitString(String sInput, String sSeparator)

Here you can find the source of splitString(String sInput, String sSeparator)

Description

Splits a delimited String into a ArrayList of individual components example: ArrayList v = puakma.util.Util.splitString("NSW,VIC,QLD", ",");

License

Open Source License

Return

null if either of the inputs are null

Declaration

public static ArrayList splitString(String sInput, String sSeparator) 

Method Source Code

//package com.java2s;
/** ***************************************************************
Util.java//ww w. j a  va  2s. com
Copyright (C) 2001  Brendon Upson 
http://www.wnc.net.au info@wnc.net.au
    
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
    
 *************************************************************** */

import java.util.ArrayList;

public class Main {
    /**
     * Splits a delimited String into a ArrayList of individual components
     * example: ArrayList v = puakma.util.Util.splitString("NSW,VIC,QLD", ",");
     * @return null if either of the inputs are null
     */
    public static ArrayList splitString(String sInput, String sSeparator) {
        if (sInput == null || sSeparator == null)
            return null;
        ArrayList vReturn = new ArrayList();

        int iPos = sInput.indexOf(sSeparator);
        while (iPos >= 0) {
            String szToken = sInput.substring(0, iPos);
            sInput = sInput.substring(iPos + sSeparator.length(), sInput.length());
            vReturn.add(szToken);
            iPos = sInput.indexOf(sSeparator);
        }
        if (sInput.length() != 0)
            vReturn.add(sInput);

        return vReturn;
    }

    /**
     * Splits a delimited String into a ArrayList of individual components.
     * This verison uses an arraylist and a single char, so is a little faster.
     * example: ArrayList a = puakma.util.Util.splitString("NSW,VIC,QLD", ',');
     * @return null if either of the inputs are null
     */
    public static ArrayList splitString(String sInput, char cChar) {
        if (sInput == null)
            return null;
        ArrayList vReturn = new ArrayList();

        int iPos = sInput.indexOf(cChar);
        while (iPos >= 0) {
            String szToken = sInput.substring(0, iPos);
            sInput = sInput.substring(iPos + 1, sInput.length());
            vReturn.add(szToken);
            iPos = sInput.indexOf(cChar);
        }
        if (sInput.length() != 0)
            vReturn.add(sInput);

        return vReturn;
    }

    /**
     * Returns the position of one byte array within another.
     * @param src
     * @param find
     * @return -1 if not found
     */
    public static int indexOf(byte src[], byte find[]) {
        if (src == null || find == null || find.length == 0)
            return -1;
        int i = -1;
        for (i = 0; i < src.length; i++) {
            if (src[i] == find[0]) {
                boolean bOK = true;
                for (int k = 0; k < find.length; k++) {
                    int iSrcPos = i + k;
                    if (iSrcPos >= src.length)
                        return -1;
                    if (src[iSrcPos] != find[k])
                        bOK = false;
                } //for k
                if (bOK)
                    return i;
            }
        } //for i

        return -1;
    }

    /**
     * Returns the position of one byte array within another.
     * @param src
     * @param find
     * @return -1 if not found
     */
    public static int indexOf(char src[], char find[]) {
        if (src == null || find == null || find.length == 0)
            return -1;
        int i = -1;
        for (i = 0; i < src.length; i++) {
            if (src[i] == find[0]) {
                boolean bOK = true;
                for (int k = 0; k < find.length; k++) {
                    int iSrcPos = i + k;
                    if (iSrcPos >= src.length)
                        return -1;
                    if (src[iSrcPos] != find[k])
                        bOK = false;
                } //for k
                if (bOK)
                    return i;
            }
        } //for i

        return -1;
    }
}

Related

  1. splitList(String source, char separator)
  2. splitListBySeparator(String text, String separator)
  3. splitNotRegex(String str, String separatorChars)
  4. splitSmart(String s, char separator)
  5. splitStaySeparator(String str, char token)
  6. splitStringToLong(String strInput, String separator)
  7. splitStringWithBracesOnSeparator(String string, char separator)
  8. splitStructs(String str, char separator)
  9. splitToList(String s, String separator)