Java String Shorten shorten(String string, int upTo)

Here you can find the source of shorten(String string, int upTo)

Description

Shorten the given string if the string is more than the given length.

License

LGPL

Parameter

Parameter Description
string the string to shorten
upTo the up to

Return

the string

Declaration

public static String shorten(String string, int upTo) 

Method Source Code

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

public class Main {
    /**//from w w  w . j a v  a 2s  . c o m
     * Shorten the given string if the string is more than the given length.
     * <p>
     * Example:<br>
     * <code>
     * System.out.println(LoggingUtil.shorten("The quick brown fox jumps over the lazy dog", 10));
     * </code><br>
     * Prints:<br>
     * <code>The quick... 33 more</code>
     *
     * @param string
     *            the string to shorten
     * @param upTo
     *            the up to
     * @return the string
     */
    public static String shorten(String string, int upTo) {
        if (string.length() > upTo) {
            StringBuilder builder = new StringBuilder(upTo + 10);
            return builder.append(string.substring(0, upTo)).append("... ").append(string.length() - upTo)
                    .append(" more").toString();
        }
        return string;
    }
}

Related

  1. shorten(String script, int length)
  2. shorten(String str, int length)
  3. shorten(String str, int length)
  4. shorten(String string, boolean isPrefix)
  5. shorten(String string, int size)
  6. shorten(String text, int max)
  7. shorten(String text, int size, int mode)
  8. shorten(String[] a, int from, int to)
  9. shortenAddress(String address)