Here you can find the source of truncateString(String str, int toLen)
str
to given length toLen
.
Parameter | Description |
---|---|
str | string to truncate |
toLen | which the length of output string should be less than, negative or zero mean clear the string |
toLen
is negative then return empty string.
public static String truncateString(String str, int toLen)
//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(); } }