Java String Abbreviate abbreviateMiddle(String string, int maxLength)

Here you can find the source of abbreviateMiddle(String string, int maxLength)

Description

abbreviate Middle

License

Open Source License

Declaration

public static String abbreviateMiddle(String string, int maxLength) 

Method Source Code

//package com.java2s;

public class Main {
    public static final String ELLIPSIS = "...";

    public static String abbreviateMiddle(String string, int maxLength) {
        if (string == null || maxLength >= string.length()) {
            return string;
        }//from w  w w . j a  v  a2 s  . co m

        final int targetSting = maxLength - ELLIPSIS.length();
        final int startOffset = targetSting / 2 + targetSting % 2;
        final int endOffset = string.length() - targetSting / 2;

        return string.substring(0, startOffset) + ELLIPSIS + string.substring(endOffset);
    }
}

Related

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