Java String Ellipse ellipsize(String text, int maxLength)

Here you can find the source of ellipsize(String text, int maxLength)

Description

Cuts the string at the end if it's longer than maxLength and appends "..."

License

Apache License

Declaration

public static String ellipsize(String text, int maxLength) 

Method Source Code

//package com.java2s;
/*/*from w  w w . ja v a2  s  .  c  o  m*/
 * Copyright (C) 2014 Markus Junginger, greenrobot (http://greenrobot.de)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Cuts the string at the end if it's longer than maxLength and appends "..." to it. The length of the resulting
     * string including "..." is always less or equal to the given maxLength. It's valid to pass a null text; in this
     * case null is returned.
     */
    public static String ellipsize(String text, int maxLength) {
        if (text != null && text.length() > maxLength) {
            return text.substring(0, maxLength - 3) + "...";
        }
        return text;
    }
}

Related

  1. ellipsize(String input, int maxLength)
  2. ellipsize(String s, int maxChars)
  3. ellipsize(String string, int length)
  4. ellipsize(String text, int max)
  5. ellipsize(String text, int max)
  6. ellipsizeKeepingExtension(String s, int maxChars)