Java String Split by Separator split(String array, String separators)

Here you can find the source of split(String array, String separators)

Description

split

License

Apache License

Parameter

Parameter Description
array a parameter
separators a parameter

Return

element trimmed in array elements

Declaration

public static String[] split(String array, String separators) 

Method Source Code

//package com.java2s;
/**/*from   w  ww  .  ja va2 s.  c  o  m*/
 *  Copyright 2011 Marco Berri - marcoberri@gmail.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.List;

public class Main {
    /**
     * 
     * @param array
     * @param separators
     * @return element trimmed in array elements
     */
    public static String[] split(String array, String separators) {
        final List list = splitAsList(array, separators);
        return (String[]) list.toArray(new String[list.size()]);
    }

    /**
     * 
     * @param array
     * @param separators
     * @return List
     */
    public static List splitAsList(String array, String separators) {
        List list = new ArrayList();

        if (array == null) {
            return list;
        }

        if (separators == null) {
            separators = "\\s+";
        } else {
            separators = "[" + separators + "]+";
        }

        String s[] = array.split(separators);
        if (s == null || s.length == 0) {
            return list;
        }

        for (int i = 0; i < s.length; i++) {
            if (!isEmpty(s[i])) {
                list.add(s[i].trim());
            }
        }

        return list;
    }

    /**
     * Checks if a String is empty or null
     * 
     * @param s
     *            String to check
     * @return true if null or empty, false otherwise
     */
    public static boolean isEmpty(String s) {
        return (s == null || "".equals(s.trim()));
    }

    /**
     * 
     * @param s
     * @param alt
     * @return
     */
    public static String isEmpty(String s, String alt) {
        if (isEmpty(s)) {
            return alt;
        }
        return s;
    }

    /**
     * Esegue una Trim su una Stringa se s == null return null
     * 
     * @param s
     *            String to trim
     * @return La Stringa Trimmata
     */
    public static final String trim(String s) {
        if (s == null) {
            return null;
        }
        return s.trim();
    }
}

Related

  1. split(final String str, final char separatorChar)
  2. split(final String str, final char[] separators)
  3. split(final String text, final char separator)
  4. split(final String text, final String separator)
  5. split(final T string, final char separator)
  6. split(String input, int separator)
  7. split(String left, String separator)
  8. split(String s, char separator)
  9. split(String s, char separator)