Java String Truncate truncateString(String str, int toLen)

Here you can find the source of truncateString(String str, int toLen)

Description

Truncate a given string str to given length toLen.

License

Apache License

Parameter

Parameter Description
str string to truncate
toLen which the length of output string should be less than, negative or zero mean clear the string

Return

return truncated string if the given string is valid ,otherwise return input string itself; if the input string is valid and the toLen is negative then return empty string.

Declaration

public static String truncateString(String str, int toLen) 

Method Source Code

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

public class Main {
    /**//from  w w w.  jav a 2 s . c o m
     * Truncate a given string <code>str</code> to given length <code>toLen</code>.
     * <p>
     * Examples:
     * <blockquote><pre>
     *  StringUtil.truncateString("Hello world", 5) return "Hello"
     *  StringUtil.truncateString("") return ""
     *  StringUtil.truncateString("Hello world", 20) return "Hello world"
     *  StringUtil.truncateString("Hello world", -1) return ""
     * </pre></blockquote>
     * @param str string to truncate
     * @param toLen which the length of output string should be less than,
     * negative or zero mean clear the string
     * @return return truncated string if the given string is valid ,otherwise
     * return input string itself; if the input string is valid and the 
     * <code>toLen</code> is negative then return empty string.
     */
    public static String truncateString(String str, int toLen) {
        String newStr = null;

        if (null == str) {
            return str;
        }

        if (toLen <= 0) {
            return "";
        }

        if (str.length() > toLen) {
            newStr = str.substring(0, toLen);
        } else {
            return str;
        }

        return newStr;
    }

    /**
     * Truncate the given string to  given length <code>toLen</code> and append
     * given suffix <code>suffix</code> to it. 
     * <br>Note: the <code>toLen</code> not include the length of <code>suffix</code>
     * <p>
     * <blockquote><pre>
     *  StringUtil.truncateString("Hello world", 5) return "Hello..."
     *  StringUtil.truncateString("") return ""
     *  StringUtil.truncateString("Hello world", 20) return "Hello world..."
     *  StringUtil.truncateString("Hello world", -1) return "..."
     * </pre></blockquote>
     * @param str string to truncate
     * @param toLen which the length of output string should less than exclude suffix     
     * @param suffix the suffix append to the output string, default value is "..."
     * @return return truncated string if the given string is valid ,otherwise
     * return input string itself; if the input string is valid and the 
     * <code>toLen</code> is negative then return empty string.
     */
    public static String truncateString(String str, int toLen, String suffix) {
        if (null == str) {
            return str;
        }

        if (null == suffix) {
            suffix = "...";
        }

        StringBuffer newStr = new StringBuffer(toLen + suffix.length());

        newStr.append(truncateString(str, toLen));

        if (toLen < str.length()) {
            newStr.append(suffix);
        }

        return newStr.toString();
    }

    /**
     * Get string value of given Object <code>param</code>.
     * This method will invoke toString() method on given object if not null.
     * @param param object instance
     * @return string represent given object if it's not null, otherwise return null.
     */
    public static String toString(Object param) {
        if (param == null) {
            return null;
        }

        return param.toString().trim();
    }
}

Related

  1. truncateString(String source, String suffix)
  2. truncateString(String str, int length)
  3. truncateString(String str, int length, String suffix)
  4. truncateString(String str, int maxlen)
  5. truncateString(String str, int size)
  6. truncateString(String string, int maxLength)
  7. truncateString(String text, int truncateAt)
  8. truncateStringAt(String source, int len)
  9. truncateStringChars(String strIn, String substituteChars, int maxChars, boolean keepRightSide)