Java String Abbreviate abbreviate(String str)

Here you can find the source of abbreviate(String str)

Description

Abbreviates a string such that it doesn't straddle more than one line, and isn't very long.

License

Open Source License

Parameter

Parameter Description
str The string which is to be abbreviated.

Return

The abbreviated string or str if no abbreviation was needed.

Declaration

public static String abbreviate(String str) 

Method Source Code

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

public class Main {
    /**//from  w  w  w  .  j  a va2s.com
     * Abbreviates a string such that it doesn't straddle more than one line, and isn't very long.
     * Useful in toString methods where one attribute may be long, and you want to keep the output concise.
     * 
     * @param str
     *            The string which is to be abbreviated.
     * @return The abbreviated string or str if no abbreviation was needed.
     */
    public static String abbreviate(String str) {
        return abbreviate(str, 60);
    }

    public static String abbreviate(String str, int limit) {
        if (str == null) {
            return null;
        }
        if (str.length() > limit) {
            str = str.substring(0, limit) + "...";
        }
        int nl = str.indexOf("\n");
        if (nl >= 0) {
            str = str.substring(0, nl) + "\\n...";
        }

        return str;
    }
}

Related

  1. abbreviate(String name)
  2. abbreviate(String s, int length)
  3. abbreviate(String s, int max)
  4. abbreviate(String s, int maxLength)
  5. abbreviate(String src, int maxlen, String replacement)
  6. abbreviate(String str, int maximumChars)
  7. abbreviate(String str, int maxlen, String appendIfLimit)
  8. abbreviate(String str, int maxWidth)
  9. abbreviate(String str, int maxWidth)