Java String Abbreviate abbreviate(String str, int maxlen, String appendIfLimit)

Here you can find the source of abbreviate(String str, int maxlen, String appendIfLimit)

Description

Return abbreviated string to maxlen length.

License

Apache License

Parameter

Parameter Description
str string to be limited
maxlen max length of resulting string
appendIfLimit string to be appended if string is limited

Return

string with length not greater than maxlen with last few characters replaced with appendIfLimit

Declaration

public static String abbreviate(String str, int maxlen, String appendIfLimit) 

Method Source Code

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

public class Main {
    /**/*from   w w w  .j  a  v a  2 s . c om*/
     * Return abbreviated string to maxlen length. Resulted string has no more than
     * maxlimit characters (including appended appendIfLimit string).
     * 
     * Examples:
     * abbreviate("abcdefghij", 20, "..."); // returns "abcdefghij"
     * abbreviate("abcdefghij", 10, "..."); // returns "abcdefghij"
     * abbreviate("abcdefghij",  9, "..."); // returns "abcdef..."
     * abbreviate("abcdefghij",  5, null);  // returns "abcde"
     * 
     * @param str string to be limited
     * @param maxlen max length of resulting string
     * @param appendIfLimit string to be appended if string is limited
     * @return string with length not greater than maxlen with last few characters replaced with appendIfLimit
     */
    public static String abbreviate(String str, int maxlen, String appendIfLimit) {
        if (appendIfLimit == null)
            appendIfLimit = "";
        if (str.length() > maxlen) {
            return str.substring(0, maxlen - appendIfLimit.length()) + appendIfLimit;
        }
        return str;
    }
}

Related

  1. abbreviate(String s, int max)
  2. abbreviate(String s, int maxLength)
  3. abbreviate(String src, int maxlen, String replacement)
  4. abbreviate(String str)
  5. abbreviate(String str, int maximumChars)
  6. abbreviate(String str, int maxWidth)
  7. abbreviate(String str, int maxWidth)
  8. abbreviate(String str, int maxWidth)
  9. abbreviate(String str, int maxWidth)