Java String Shorten shorten(String s)

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

Description

shorten

License

Open Source License

Declaration

public static String shorten(String s) 

Method Source Code

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

public class Main {
    public static String shorten(String s) {
        return shorten(s, 20);
    }//from  www.  j ava  2 s  . c om

    public static String shorten(String s, int length) {
        return shorten(s, length, "...");
    }

    public static String shorten(String s, int length, String suffix) {
        if ((s == null) || (suffix == null)) {
            return null;
        }

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

                    break;
                }
            }

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

            s = temp.concat(suffix);
        }

        return s;
    }

    public static int length(String s) {
        if (s == null) {
            return 0;
        }
        return s.length();
    }
}

Related

  1. shorten(String nameForUI, int maxLen)
  2. shorten(String pkg, boolean shorten)
  3. shorten(String s)
  4. shorten(String s)
  5. shorten(String s)
  6. shorten(String s, int len)
  7. shorten(String s, int length)
  8. shorten(String s, int length, String suffix)
  9. shorten(String s, int length, String suffix)