Java String Ellipse ellipsize(String string, int length)

Here you can find the source of ellipsize(String string, int length)

Description

Ellipsize a string, keeping it readable

License

Open Source License

Parameter

Parameter Description
string of text
length to cut string down to

Return

The ellipsied string.

Declaration

public static String ellipsize(String string, int length) 

Method Source Code

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

public class Main {
    /**//from  ww w .j  a v  a 2  s .co  m
     * Ellipsize a string, keeping it readable
     * 
     * @param string
     *            of text
     * @param length
     *            to cut string down to
     * @return The ellipsied string.
     */
    public static String ellipsize(String string, int length) {
        String ellipsis = "...";

        if (length >= string.length()) {
            return string;
        }

        int trim = length - ellipsis.length();
        return string.substring(0, trim / 2) + ellipsis + string.substring(string.length() - trim / 2);
    }
}

Related

  1. ellipsisString(String string, int len)
  2. ellipsisString(String text, int ellipsisAt)
  3. ellipsisText(String text, int maxLength)
  4. ellipsize(String input, int maxLength)
  5. ellipsize(String s, int maxChars)
  6. ellipsize(String text, int max)
  7. ellipsize(String text, int max)
  8. ellipsize(String text, int maxLength)
  9. ellipsizeKeepingExtension(String s, int maxChars)