ellipsize in Middle of a string - Android java.lang

Android examples for java.lang:String Shorten

Description

ellipsize in Middle of a string

Demo Code

public class Main {

  public static String ellipsizeMiddle(String input, int max, boolean padLeft) {
    if (input == null || input.length() < max) {
      return (padLeft ? padLeft(input, max) : input);
    }//from  w  w  w  .ja  v  a 2  s . co m
    int charactersAfterEllipsis = max / 2;
    int charactersBeforeEllipsis = max - 3 - charactersAfterEllipsis;
    return input.substring(0, charactersBeforeEllipsis) + "..."
        + input.substring(input.length() - charactersAfterEllipsis);
  }

  public static String padLeft(String s, int n) {
    return String.format("%1$" + n + "s", s);
  }

}

Related Tutorials