Java String Empty trimToEmpty(String value)

Here you can find the source of trimToEmpty(String value)

Description

Trims the given value , returns empty if null .

License

Apache License

Parameter

Parameter Description
value The value to trim.

Return

the given value trimmed or empty if null .

Declaration

public static String trimToEmpty(String value) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    /**/*from  w w w . ja v  a  2s . c om*/
     * Trims the given {@code value}, returns {@code empty} if {@code null}.
     *
     * @param value
     *         The value to trim.
     * @return the given {@code value} trimmed or {@code empty} if {@code null}.
     */
    public static String trimToEmpty(String value) {
        if (isBlank(value)) {
            return "";
        }
        return value.trim();
    }

    /**
     * Returns if the given {@code value} is blank.
     * <p/>
     * <pre>
     * isBlank(null)    -> true
     * isBlank("")      -> true
     * isBlank(" ")     -> true
     * isBlank(" test") -> false
     * isBlank(" a ")   -> false
     * </pre>
     *
     * @param value
     *         The value to test.
     * @return {@code true} if the given {@code value} is {@code null} or {@code empty}.
     */
    public static boolean isBlank(String value) {
        return value == null || value.trim().isEmpty();
    }

    /**
     * Trims the given {@code value}.
     *
     * @param value
     *         The value to trim.
     * @return the given {@code value} trimmed or {@code null}.
     */
    public static String trim(String value) {
        if (value == null) {
            return null;
        }
        return value.trim();
    }

    /**
     * Returns if the given {@code collection} is {@code null} or empty.
     *
     * @param collection
     *         The collection to test.
     * @return {@code true} if the given {@code collection} is {@code null} or {@code empty}.
     */
    public static <C extends Collection<?>> boolean isEmpty(C collection) {
        return collection == null || collection.isEmpty();
    }

    /**
     * Returns if the given {@code array} is {@code null} or empty.
     *
     * @param array
     *         The array to test.
     * @return {@code true} if the given {@code array} is {@code null} or {@code empty}.
     */
    public static <T extends Object> boolean isEmpty(T[] array) {
        return array == null || array.length == 0;
    }

    /**
     * Returns if the given {@code map} is {@code null} or empty.
     *
     * @param map
     *         The map to test.
     * @return {@code true} if the given {@code map} is {@code null} or {@code empty}.
     */
    public static <M extends Map<?, ?>> boolean isEmpty(M map) {
        return map == null || map.isEmpty();
    }
}

Related

  1. isNullOrEmpty(String message, String string)
  2. isNullOrEmpty(String value)
  3. splitAt(String s, String delimiter, boolean includeEmpty)
  4. tokenizeToStringArray(String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens)
  5. tokenizeToStringArray(String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens)