Java String Abbreviate abbreviate(String _str, int _length)

Here you can find the source of abbreviate(String _str, int _length)

Description

Abbreviates a String using ellipses.

License

Open Source License

Parameter

Parameter Description
_str string to abbrivate
_length max length

Return

abbreviated string, original string if string length is lower or equal then desired length or null if input was null

Declaration

public static String abbreviate(String _str, int _length) 

Method Source Code

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

public class Main {
    /**//w  w  w .  j  av a 2  s  .  co  m
     * Abbreviates a String using ellipses.
     *
     * @param _str string to abbrivate
     * @param _length max length
     * @return abbreviated string, original string if string length is lower or equal then desired length or null if input was null
     */
    public static String abbreviate(String _str, int _length) {
        if (_str == null) {
            return null;
        }
        if (_str.length() <= _length) {
            return _str;
        }

        String abbr = _str.substring(0, _length - 3) + "...";

        return abbr;
    }
}

Related

  1. abbreviate(final String str)
  2. abbreviate(final String str, final int maxWidth)
  3. abbreviate(final String str, int lower, int upper, final String appendToEnd)
  4. abbreviate(final String value, final int maxLength)
  5. abbreviate(final String value, int max)
  6. abbreviate(String content, int maxWidth, String enc, String suffix)
  7. abbreviate(String fileName, int maxLen)
  8. abbreviate(String input, int length)
  9. abbreviate(String longName)