Java String Split splitStringBySeperator(final String str, final String seperator)

Here you can find the source of splitStringBySeperator(final String str, final String seperator)

Description

split String By Seperator

License

Open Source License

Declaration

public static String[] splitStringBySeperator(final String str, final String seperator) 

Method Source Code

//package com.java2s;
/*//from  ww  w  .  jav  a2s  . c  o m
 * Copyright (c) 2011 Tommy Andersen and Mikkel Wendt-Larsen
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import java.util.ArrayList;

public class Main {
    public static String[] splitStringBySeperator(final String str, final String seperator) {
        if (str == null) {
            return null;
        }

        String sep = seperator;
        if (sep == null) {
            sep = " ";
        }

        if (sep.equals("")) {
            return new String[] { str };
        }

        int len = str.length();
        int start = 0;
        int pos = str.indexOf(sep, start);

        ArrayList<String> substrings = new ArrayList<String>();

        while (pos > start) {
            substrings.add(str.substring(start, pos));
            start = pos + sep.length();
            pos = str.indexOf(sep, start);
        }

        substrings.add(str.substring(start, str.length()));

        return substrings.toArray(new String[substrings.size()]);
    }
}

Related

  1. splitString(String s)
  2. splitString(String s)
  3. splitString(String src, String target)
  4. splitString(String string)
  5. splitString(String string, int limit)
  6. splitStrings(String start, String end, int numSplitStrings, boolean oddLevel, List splits)
  7. splitStringToList(String pstrString, String pstrSep)
  8. splitStringWithoutTrim(String strInput, String strSplitor)
  9. splitStruct(String struct)