Java String Split by Delimiter splitString(String toSplit, String delimiter)

Here you can find the source of splitString(String toSplit, String delimiter)

Description

Split a String at the first occurrence of the delimiter.

License

Open Source License

Parameter

Parameter Description
toSplit the string to split
delimiter to split the string up with

Return

a two element array with index 0 being before the delimiter, and index 1 being after the delimiter (neither element includes the delimiter); or null if the delimiter wasn't found in the given input String

Declaration

public static List<String> splitString(String toSplit, String delimiter) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    /**//from  ww w . j  a v  a  2  s . c om
     * Split a String at the first occurrence of the delimiter.
     * Does not include the delimiter in the result.
     * @param toSplit the string to split
     * @param delimiter to split the string up with
     * @return a two element array with index 0 being before the delimiter, and
     * index 1 being after the delimiter (neither element includes the delimiter);
     * or <code>null</code> if the delimiter wasn't found in the given input String
     */
    public static List<String> splitString(String toSplit, String delimiter) {
        if (!hasLength(toSplit) || !hasLength(delimiter)) {
            return Collections.emptyList();
        }

        return Arrays.asList(toSplit.split(delimiter));
    }

    /**
     * Check that the given CharSequence is neither <code>null</code> nor of length 0.
     * Note: Will return <code>true</code> for a CharSequence that purely consists of whitespace.
     * <p><pre>
     * StringUtils.hasLength(null) = false
     * StringUtils.hasLength("") = false
     * StringUtils.hasLength(" ") = true
     * StringUtils.hasLength("Hello") = true
     * </pre>
     * @param str the CharSequence to check (may be <code>null</code>)
     * @return <code>true</code> if the CharSequence is not null and has length
     */
    public static boolean hasLength(CharSequence str) {
        return (str != null && str.length() > 0);
    }
}

Related

  1. splitString(String splitStr, String delim)
  2. splitString(String str, char delim)
  3. splitString(String str, String delim)
  4. splitString(String str, String delimiter)
  5. splitString(String str, String delims)
  6. splitStringList(String strList, String delimit)
  7. splitStringUsingDelimiter(String name, String delimiter)
  8. splitText(String text, String delimiter)
  9. splitToArray(String stringToSplit, String delimitter, boolean trim)