Java String Split splitString(String s)

Here you can find the source of splitString(String s)

Description

split String

License

Open Source License

Declaration

public static String[] splitString(String s) 

Method Source Code

//package com.java2s;
/**//from  ww w. j a  v a  2s . co  m
 * This file is part of SIMPL4(http://simpl4.org).
 *
 *    Copyright [2014] [Manfred Sattler] <manfred@ms123.org>
 *
 * SIMPL4 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 3 of the License, or
 * (at your option) any later version.
 *
 * SIMPL4 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 SIMPL4.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.*;

public class Main {
    private static char[] delimeters = { ' ', '-', '+', ',', '/' };

    public static String[] splitString(String s) {
        if (indexOfAny(s, delimeters) == -1) {
            String[] sa = new String[1];
            sa[0] = s;
            return sa;
        }
        StringTokenizer st = new StringTokenizer(s, " +-,/");
        String[] sp = new String[st.countTokens()];
        int i = 0;
        while (st.hasMoreTokens()) {
            sp[i++] = st.nextToken();
        }
        return sp;
    }

    private static int indexOfAny(String str, char[] searchChars) {
        if (str == null) {
            return -1;
        }
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            for (char searchChar : searchChars) {
                if (searchChar == ch) {
                    return i;
                }
            }
        }
        return -1;
    }
}

Related

  1. splitStep(String step)
  2. splitStr(String str, int toCount)
  3. splitString(String bigString, String splitter)
  4. splitString(String hexString, int partSize)
  5. splitString(String s)
  6. splitString(String s)
  7. splitString(String src, String target)
  8. splitString(String string)
  9. splitString(String string, int limit)