Java String Truncate truncateWithDefault(String value, int size, String defaultValue)

Here you can find the source of truncateWithDefault(String value, int size, String defaultValue)

Description

truncateWithDefault.

License

Open Source License

Parameter

Parameter Description
value value
size size
defaultValue defaultValue

Return

String

Declaration

public static String truncateWithDefault(String value, int size, String defaultValue) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*  ww w . j  a  v  a 2 s .c o  m*/
     * truncateWithDefault.
     * @param value value
     * @param size size
     * @param defaultValue defaultValue
     * @return String
     */
    public static String truncateWithDefault(String value, int size, String defaultValue) {
        if (isEmpty(value)) {
            return defaultValue;
        }
        if (value.length() > size) {
            return defaultValue;
        }
        return value;
    }

    /**
     * whether or not the string is null or consists only of whitespace. 
     * @param s 
     * @return boolean
     * */
    public static boolean isEmpty(String s) {
        return s == null || s.trim().length() == 0;
    }
}

Related

  1. truncateUrl(String url)
  2. TruncateUrlPage(String strURL)
  3. truncateValue(String value, int limit, String endChars)
  4. truncateWhenUTF8(final String s, final int maxBytes)
  5. truncateWhiteSpace(int len, int tabSize, String indentStr)
  6. truncateWithEllipsis(String str, int maxLength)
  7. truncAtWord(String a_text, int a_length)
  8. truncString(String in, int len)
  9. truncuate(String s, int maximumLength, String append)