Java String Shorten shorten(String text, int max)

Here you can find the source of shorten(String text, int max)

Description

shorten

License

Apache License

Declaration

public static String shorten(String text, int max) 

Method Source Code

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

public class Main {
    public static String shorten(String text, int max) {
        // case 1: nothing
        if (text == null)
            return "";

        // case 2: it fits
        if (text.length() <= max)
            return text;

        // case 3: max is short, return what we can
        if (max <= 5)
            return text.substring(0, max);

        // case 4: max is long, return what we can and append ..
        return text.substring(0, max - 2) + "..";
    }/*w  ww.j av a2s  .  c o  m*/
}

Related

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