Java String Abbreviate abbreviateMiddle(String str, String middle, int length)

Here you can find the source of abbreviateMiddle(String str, String middle, int length)

Description

Abbreviates a String to the length passed, replacing the middle characters with the supplied replacement String.

This abbreviation only occurs if the following criteria is met:

  • Neither the String for abbreviation nor the replacement String are null or empty
  • The length to truncate to is less than the length of the supplied String
  • The length to truncate to is greater than 0
  • The abbreviated String will have enough room for the length supplied replacement String and the first and last characters of the supplied String for abbreviation
Otherwise, the returned String will be the same as the supplied String for abbreviation.

License

Apache License

Parameter

Parameter Description
str the String to abbreviate, may be null
middle the String to replace the middle characters with, may be null
length the length to abbreviate <code>str</code> to.

Return

the abbreviated String if the above criteria is met, or the original String supplied for abbreviation.

Declaration

public static String abbreviateMiddle(String str, String middle, int length) 

Method Source Code

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

public class Main {
    /**/*  w w w.  ja  v  a  2s  . c  o  m*/
     * The empty String <code>""</code>.
     *
     * @since 2.0
     */
    public static final String EMPTY = "";

    /**
     * <p>Abbreviates a String to the length passed, replacing the middle characters with the supplied
     * replacement String.</p>
     * <p>
     * <p>This abbreviation only occurs if the following criteria is met:
     * <ul>
     * <li>Neither the String for abbreviation nor the replacement String are null or empty </li>
     * <li>The length to truncate to is less than the length of the supplied String</li>
     * <li>The length to truncate to is greater than 0</li>
     * <li>The abbreviated String will have enough room for the length supplied replacement String
     * and the first and last characters of the supplied String for abbreviation</li>
     * </ul>
     * Otherwise, the returned String will be the same as the supplied String for abbreviation.
     * </p>
     * <p>
     * <pre>
     * BConfigStringUtil.abbreviateMiddle(null, null, 0)      = null
     * BConfigStringUtil.abbreviateMiddle("abc", null, 0)      = "abc"
     * BConfigStringUtil.abbreviateMiddle("abc", ".", 0)      = "abc"
     * BConfigStringUtil.abbreviateMiddle("abc", ".", 3)      = "abc"
     * BConfigStringUtil.abbreviateMiddle("abcdef", ".", 4)     = "ab.f"
     * </pre>
     *
     * @param str    the String to abbreviate, may be null
     * @param middle the String to replace the middle characters with, may be null
     * @param length the length to abbreviate <code>str</code> to.
     *
     * @return the abbreviated String if the above criteria is met, or the original String supplied for abbreviation.
     *
     * @since 2.5
     */
    public static String abbreviateMiddle(String str, String middle, int length) {
        if (isEmpty(str) || isEmpty(middle)) {
            return str;
        }

        if (length >= str.length() || length < (middle.length() + 2)) {
            return str;
        }

        int targetSting = length - middle.length();
        int startOffset = targetSting / 2 + targetSting % 2;
        int endOffset = str.length() - targetSting / 2;

        StringBuffer builder = new StringBuffer(length);
        builder.append(str.substring(0, startOffset));
        builder.append(middle);
        builder.append(str.substring(endOffset));

        return builder.toString();
    }

    /**
     * <p>Checks if a String is empty ("") or null.</p>
     * <p>
     * <pre>
     * BConfigStringUtil.isEmpty(null)      = true
     * BConfigStringUtil.isEmpty("")        = true
     * BConfigStringUtil.isEmpty(" ")       = false
     * BConfigStringUtil.isEmpty("bob")     = false
     * BConfigStringUtil.isEmpty("  bob  ") = false
     * </pre>
     * <p>
     * <p>NOTE: This method changed in Lang version 2.0.
     * It no longer trims the String.
     * That functionality is available in isBlank().</p>
     *
     * @param str the String to check, may be null
     *
     * @return <code>true</code> if the String is empty or null
     */
    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    /**
     * Gets a String's length or <code>0</code> if the String is <code>null</code>.
     *
     * @param str a String or <code>null</code>
     *
     * @return String length or <code>0</code> if the String is <code>null</code>.
     *
     * @since 2.4
     */
    public static int length(String str) {
        return str == null ? 0 : str.length();
    }

    /**
     * <p>Gets a substring from the specified String avoiding exceptions.</p>
     * <p>
     * <p>A negative start position can be used to start <code>n</code>
     * characters from the end of the String.</p>
     * <p>
     * <p>A <code>null</code> String will return <code>null</code>.
     * An empty ("") String will return "".</p>
     * <p>
     * <pre>
     * BConfigStringUtil.substring(null, *)   = null
     * BConfigStringUtil.substring("", *)     = ""
     * BConfigStringUtil.substring("abc", 0)  = "abc"
     * BConfigStringUtil.substring("abc", 2)  = "c"
     * BConfigStringUtil.substring("abc", 4)  = ""
     * BConfigStringUtil.substring("abc", -2) = "bc"
     * BConfigStringUtil.substring("abc", -4) = "abc"
     * </pre>
     *
     * @param str   the String to get the substring from, may be null
     * @param start the position to start from, negative means
     *              count back from the end of the String by this many characters
     *
     * @return substring from start position, <code>null</code> if null String input
     */
    public static String substring(String str, int start) {
        if (str == null) {
            return null;
        }

        // handle negatives, which means last n characters
        if (start < 0) {
            start = str.length() + start; // remember start is negative
        }

        if (start < 0) {
            start = 0;
        }
        if (start > str.length()) {
            return EMPTY;
        }

        return str.substring(start);
    }

    /**
     * <p>Gets a substring from the specified String avoiding exceptions.</p>
     * <p>
     * <p>A negative start position can be used to start/end <code>n</code>
     * characters from the end of the String.</p>
     * <p>
     * <p>The returned substring starts with the character in the <code>start</code>
     * position and ends before the <code>end</code> position. All position counting is
     * zero-based -- i.e., to start at the beginning of the string use
     * <code>start = 0</code>. Negative start and end positions can be used to
     * specify offsets relative to the end of the String.</p>
     * <p>
     * <p>If <code>start</code> is not strictly to the left of <code>end</code>, ""
     * is returned.</p>
     * <p>
     * <pre>
     * BConfigStringUtil.substring(null, *, *)    = null
     * BConfigStringUtil.substring("", * ,  *)    = "";
     * BConfigStringUtil.substring("abc", 0, 2)   = "ab"
     * BConfigStringUtil.substring("abc", 2, 0)   = ""
     * BConfigStringUtil.substring("abc", 2, 4)   = "c"
     * BConfigStringUtil.substring("abc", 4, 6)   = ""
     * BConfigStringUtil.substring("abc", 2, 2)   = ""
     * BConfigStringUtil.substring("abc", -2, -1) = "b"
     * BConfigStringUtil.substring("abc", -4, 2)  = "ab"
     * </pre>
     *
     * @param str   the String to get the substring from, may be null
     * @param start the position to start from, negative means
     *              count back from the end of the String by this many characters
     * @param end   the position to end at (exclusive), negative means
     *              count back from the end of the String by this many characters
     *
     * @return substring from start position to end positon,
     * <code>null</code> if null String input
     */
    public static String substring(String str, int start, int end) {
        if (str == null) {
            return null;
        }

        // handle negatives
        if (end < 0) {
            end = str.length() + end; // remember end is negative
        }
        if (start < 0) {
            start = str.length() + start; // remember start is negative
        }

        // check length next
        if (end > str.length()) {
            end = str.length();
        }

        // if start is greater than end, return ""
        if (start > end) {
            return EMPTY;
        }

        if (start < 0) {
            start = 0;
        }
        if (end < 0) {
            end = 0;
        }

        return str.substring(start, end);
    }
}

Related

  1. abbreviateDotSeparatedString(String string, int targetLength)
  2. abbreviateFileName(final String fileName)
  3. abbreviateInCenter(String stringToAbbreviate, int length)
  4. abbreviateLeft(String s, int width)
  5. abbreviateMessage(String messageBody)
  6. abbreviateMiddle(String string, int maxLength)
  7. abbreviateObj(Object value, int maxWidth)
  8. abbreviateScript(String script)
  9. abbreviateText(final String text, final int numberOfCharacters, final String appendText)