
com.github.yt.commons.util.YtStringUtils Maven / Gradle / Ivy
package com.github.yt.commons.util;
import java.util.Iterator;
/**
* copy from org.apache.commons
*
* @author org.apache.commons
*/
public class YtStringUtils {
/**
* The empty String {@code ""}.
*
* @since 2.0
*/
public static final String EMPTY = "";
/**
* Represents a failed index search.
*
* @since 2.1
*/
public static final int INDEX_NOT_FOUND = -1;
/**
* Checks if a CharSequence is empty (""), null or whitespace only.
*
* Whitespace is defined by {@link Character#isWhitespace(char)}.
*
*
* YtStringUtils.isBlank(null) = true
* YtStringUtils.isBlank("") = true
* YtStringUtils.isBlank(" ") = true
* YtStringUtils.isBlank("bob") = false
* YtStringUtils.isBlank(" bob ") = false
*
*
* @param cs the CharSequence to check, may be null
* @return {@code true} if the CharSequence is null, empty or whitespace only
* @since 2.0
* @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
*/
public static boolean isBlank(final CharSequence cs) {
int strLen;
if (cs == null || (strLen = cs.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(cs.charAt(i))) {
return false;
}
}
return true;
}
/**
* Checks if a CharSequence is not empty (""), not null and not whitespace only.
*
* Whitespace is defined by {@link Character#isWhitespace(char)}.
*
*
* YtStringUtils.isNotBlank(null) = false
* YtStringUtils.isNotBlank("") = false
* YtStringUtils.isNotBlank(" ") = false
* YtStringUtils.isNotBlank("bob") = true
* YtStringUtils.isNotBlank(" bob ") = true
*
*
* @param cs the CharSequence to check, may be null
* @return {@code true} if the CharSequence is
* not empty and not null and not whitespace only
* @since 2.0
* @since 3.0 Changed signature from isNotBlank(String) to isNotBlank(CharSequence)
*/
public static boolean isNotBlank(final CharSequence cs) {
return !isBlank(cs);
}
// Empty checks
//-----------------------------------------------------------------------
/**
* Checks if a CharSequence is empty ("") or null.
*
*
* YtStringUtils.isEmpty(null) = true
* YtStringUtils.isEmpty("") = true
* YtStringUtils.isEmpty(" ") = false
* YtStringUtils.isEmpty("bob") = false
* YtStringUtils.isEmpty(" bob ") = false
*
*
* NOTE: This method changed in Lang version 2.0.
* It no longer trims the CharSequence.
* That functionality is available in isBlank().
*
* @param cs the CharSequence to check, may be null
* @return {@code true} if the CharSequence is empty or null
* @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
*/
public static boolean isEmpty(final CharSequence cs) {
return cs == null || cs.length() == 0;
}
/**
* Checks if a CharSequence is not empty ("") and not null.
*
*
* YtStringUtils.isNotEmpty(null) = false
* YtStringUtils.isNotEmpty("") = false
* YtStringUtils.isNotEmpty(" ") = true
* YtStringUtils.isNotEmpty("bob") = true
* YtStringUtils.isNotEmpty(" bob ") = true
*
*
* @param cs the CharSequence to check, may be null
* @return {@code true} if the CharSequence is not empty and not null
* @since 3.0 Changed signature from isNotEmpty(String) to isNotEmpty(CharSequence)
*/
public static boolean isNotEmpty(final CharSequence cs) {
return !isEmpty(cs);
}
// Joining
//-----------------------------------------------------------------------
/**
* Joins the elements of the provided array into a single String
* containing the provided list of elements.
*
* No delimiter is added before or after the list.
* Null objects or empty strings within the array are represented by
* empty strings.
*
*
* YtStringUtils.join(null, *) = null
* YtStringUtils.join([], *) = ""
* YtStringUtils.join([null], *) = ""
* YtStringUtils.join(["a", "b", "c"], ';') = "a;b;c"
* YtStringUtils.join(["a", "b", "c"], null) = "abc"
* YtStringUtils.join([null, "", "a"], ';') = ";;a"
*
*
* @param array the array of values to join together, may be null
* @param separator the separator character to use
* @return the joined String, {@code null} if null array input
* @since 2.0
*/
public static String join(final Object[] array, final String separator) {
if (array == null) {
return null;
}
return join(array, separator, 0, array.length);
}
/**
* Joins the elements of the provided array into a single String
* containing the provided list of elements.
*
* No delimiter is added before or after the list.
* A {@code null} separator is the same as an empty String ("").
* Null objects or empty strings within the array are represented by
* empty strings.
*
*
* StringUtils.join(null, *, *, *) = null
* StringUtils.join([], *, *, *) = ""
* StringUtils.join([null], *, *, *) = ""
* StringUtils.join(["a", "b", "c"], "--", 0, 3) = "a--b--c"
* StringUtils.join(["a", "b", "c"], "--", 1, 3) = "b--c"
* StringUtils.join(["a", "b", "c"], "--", 2, 3) = "c"
* StringUtils.join(["a", "b", "c"], "--", 2, 2) = ""
* StringUtils.join(["a", "b", "c"], null, 0, 3) = "abc"
* StringUtils.join(["a", "b", "c"], "", 0, 3) = "abc"
* StringUtils.join([null, "", "a"], ',', 0, 3) = ",,a"
*
*
* @param array the array of values to join together, may be null
* @param separator the separator character to use, null treated as ""
* @param startIndex the first index to start joining from.
* @param endIndex the index to stop joining from (exclusive).
* @return the joined String, {@code null} if null array input; or the empty string
* if {@code endIndex - startIndex <= 0}. The number of joined entries is given by
* {@code endIndex - startIndex}
* @throws ArrayIndexOutOfBoundsException ife
* {@code startIndex < 0} or
* {@code startIndex >= array.length()} or
* {@code endIndex < 0} or
* {@code endIndex > array.length()}
*/
public static String join(final Object[] array, String separator, final int startIndex, final int endIndex) {
if (array == null) {
return null;
}
if (separator == null) {
separator = EMPTY;
}
// endIndex - startIndex > 0: Len = NofStrings *(len(firstString) + len(separator))
// (Assuming that all Strings are roughly equally long)
final int noOfItems = endIndex - startIndex;
if (noOfItems <= 0) {
return EMPTY;
}
final StringBuilder buf = newStringBuilder(noOfItems);
for (int i = startIndex; i < endIndex; i++) {
if (i > startIndex) {
buf.append(separator);
}
if (array[i] != null) {
buf.append(array[i]);
}
}
return buf.toString();
}
/**
* Joins the elements of the provided {@code Iterable} into
* a single String containing the provided elements.
*
* No delimiter is added before or after the list.
* A {@code null} separator is the same as an empty String ("").
*
* See the examples here: {@link #join(Object[], String)}.
*
* @param iterable the {@code Iterable} providing the values to join together, may be null
* @param separator the separator character to use, null treated as ""
* @return the joined String, {@code null} if null iterator input
* @since 2.3
*/
public static String join(Iterable> iterable, String separator) {
if (iterable == null) {
return null;
}
return join(iterable.iterator(), separator);
}
/**
* Joins the elements of the provided {@code Iterator} into
* a single String containing the provided elements.
*
* No delimiter is added before or after the list.
* A {@code null} separator is the same as an empty String ("").
*
* See the examples here: {@link #join(Object[], String)}.
*
* @param iterator the {@code Iterator} of values to join together, may be null
* @param separator the separator character to use, null treated as ""
* @return the joined String, {@code null} if null iterator input
*/
public static String join(Iterator> iterator, String separator) {
// handle null, zero and one elements before building a buffer
if (iterator == null) {
return null;
}
if (!iterator.hasNext()) {
return EMPTY;
}
Object first = iterator.next();
if (!iterator.hasNext()) {
return toString(first);
}
// two or more elements
// Java default is 16, probably too small
StringBuilder buf = new StringBuilder(256);
if (first != null) {
buf.append(first);
}
while (iterator.hasNext()) {
if (separator != null) {
buf.append(separator);
}
Object obj = iterator.next();
if (obj != null) {
buf.append(obj);
}
}
return buf.toString();
}
public static String toString(Object obj) {
return obj == null ? "" : obj.toString();
}
private static StringBuilder newStringBuilder(final int noOfItems) {
return new StringBuilder(noOfItems * 16);
}
/**
* Gets the substring after the last occurrence of a separator.
* The separator is not returned.
*
* A {@code null} string input will return {@code null}.
* An empty ("") string input will return the empty string.
* An empty or {@code null} separator will return the empty string if
* the input string is not {@code null}.
*
* If nothing is found, the empty string is returned.
*
*
* YtStringUtils.substringAfterLast(null, *) = null
* YtStringUtils.substringAfterLast("", *) = ""
* YtStringUtils.substringAfterLast(*, "") = ""
* YtStringUtils.substringAfterLast(*, null) = ""
* YtStringUtils.substringAfterLast("abc", "a") = "bc"
* YtStringUtils.substringAfterLast("abcba", "b") = "a"
* YtStringUtils.substringAfterLast("abc", "c") = ""
* YtStringUtils.substringAfterLast("a", "a") = ""
* YtStringUtils.substringAfterLast("a", "z") = ""
*
*
* @param str the String to get a substring from, may be null
* @param separator the String to search for, may be null
* @return the substring after the last occurrence of the separator,
* {@code null} if null String input
* @since 2.0
*/
public static String substringAfterLast(final String str, final String separator) {
if (isEmpty(str)) {
return str;
}
if (isEmpty(separator)) {
return EMPTY;
}
final int pos = str.lastIndexOf(separator);
if (pos == INDEX_NOT_FOUND || pos == str.length() - separator.length()) {
return EMPTY;
}
return str.substring(pos + separator.length());
}
}