Java String Shorten shorten(String s)

Here you can find the source of shorten(String s)

Description

Returns a string representing the original string appended with suffix "..."

License

Open Source License

Parameter

Parameter Description
s the original string

Return

a string representing the original string shortened to 20 characters, with suffix "..." appended to it

Declaration

public static String shorten(String s) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//  w w w  .  j  ava  2  s  .co  m
     * Returns a string representing the original string appended with suffix "..." and then shortened to 20 characters.
     * 
     * <p>
     * The suffix is only added if the original string exceeds 20 characters. If the original string exceeds 20 characters and it contains
     * whitespace, the string is shortened at the first whitespace character.
     * </p>
     * 
     * <p>
     * Examples:
     * </p>
     * 
     * <pre>
     * <code>
     * shorten("12345678901234567890xyz") returns "12345678901234567..."
     * shorten("1 345678901234567890xyz") returns "1..."
     * shorten(" 2345678901234567890xyz") returns "..."
     * shorten("12345678901234567890") returns "12345678901234567890"
     * shorten(" 2345678901234567890") returns " 2345678901234567890"
     * </code>
     * </pre>
     * 
     * @param s the original string
     * @return a string representing the original string shortened to 20 characters, with suffix "..." appended to it
     */
    public static String shorten(String s) {
        return shorten(s, 20);
    }

    /**
     * Returns a string representing the original string appended with suffix "..." and then shortened to the specified length.
     * 
     * <p>
     * The suffix is only added if the original string exceeds the specified length. If the original string exceeds the specified length and
     * it contains whitespace, the string is shortened at the first whitespace character.
     * </p>
     * 
     * <p>
     * Examples:
     * </p>
     * 
     * <pre>
     * <code>
     * shorten("123456789", 8) returns "12345..."
     * shorten("1 3456789", 8) returns "1..."
     * shorten(" 23456789", 8) returns "..."
     * shorten("12345678", 8) returns "12345678"
     * shorten(" 1234567", 8) returns " 1234567"
     * </code>
     * </pre>
     * 
     * @param s the original string
     * @param length the number of characters to limit from the original string
     * @return a string representing the original string shortened to the specified length, with suffix "..." appended to it
     */
    public static String shorten(String s, int length) {
        return shorten(s, length, "...");
    }

    /**
     * Returns a string representing the original string appended with the specified suffix and then shortened to the specified length.
     * 
     * <p>
     * The suffix is only added if the original string exceeds the specified length. If the original string exceeds the specified length and
     * it contains whitespace, the string is shortened at the first whitespace character.
     * </p>
     * 
     * <p>
     * Examples:
     * </p>
     * 
     * <pre>
     * <code>
     * shorten("12345678901234", 13, "... etc.") returns "12345... etc."
     * shorten("1 345678901234", 13, "... etc.") returns "1... etc."
     * shorten(" 2345678901234", 13, "... etc.") returns "... etc."
     * shorten("1234567890123", 13, "... etc.") returns "1234567890123"
     * shorten(" 123456789012", 13, "... etc.") returns " 123456789012"
     * </code>
     * </pre>
     * 
     * @param s the original string
     * @param length the number of characters to limit from the original string
     * @param suffix the suffix to append
     * @return a string representing the original string shortened to the specified length, with the specified suffix appended to it
     */
    public static String shorten(String s, int length, String suffix) {
        if ((s == null) || (suffix == null)) {
            return null;
        }

        if (s.length() <= length) {
            return s;
        }

        if (length < suffix.length()) {
            return s.substring(0, length);
        }

        int curLength = length;

        for (int j = (curLength - suffix.length()); j >= 0; j--) {
            if (Character.isWhitespace(s.charAt(j))) {
                curLength = j;

                break;
            }
        }

        if (curLength == length) {
            curLength = length - suffix.length();
        }

        String temp = s.substring(0, curLength);

        return temp.concat(suffix);
    }

    /**
     * Returns a string representing the original string appended with the specified suffix and then shortened to 20 characters.
     * 
     * <p>
     * The suffix is only added if the original string exceeds 20 characters. If the original string exceeds 20 characters and it contains
     * whitespace, the string is shortened at the first whitespace character.
     * </p>
     * 
     * <p>
     * Examples:
     * </p>
     * 
     * <pre>
     * <code>
     * shorten("12345678901234567890xyz", "... etc.") returns "123456789012... etc."
     * shorten("1 345678901234567890xyz", "... etc.") returns "1... etc."
     * shorten(" 2345678901234567890xyz", "... etc.") returns "... etc."
     * shorten("12345678901234567890", "... etc.") returns "12345678901234567890"
     * shorten(" 2345678901234567890", "... etc.") returns " 2345678901234567890"
     * </code>
     * </pre>
     * 
     * @param s the original string
     * @param suffix the suffix to append
     * @return a string representing the original string shortened to 20 characters, with the specified suffix appended to it
     */
    public static String shorten(String s, String suffix) {
        return shorten(s, 20, suffix);
    }
}

Related

  1. shorten(String msg, int front, String join, int end)
  2. shorten(String name, int max)
  3. shorten(String nameForUI, int maxLen)
  4. shorten(String pkg, boolean shorten)
  5. shorten(String s)
  6. shorten(String s)
  7. shorten(String s)
  8. shorten(String s, int len)
  9. shorten(String s, int length)