Java String Abbreviate abbreviate(String str, int maxWidth)

Here you can find the source of abbreviate(String str, int maxWidth)

Description

Abbreviates a string to a specified length and then adds an ellipsis if the input is greater than the maxWidth.

License

Open Source License

Parameter

Parameter Description
str the String to abbreviate.
maxWidth the maximum size of the string, minus the ellipsis.

Return

the abbreviated String, or null if the string was null.

Declaration

public static String abbreviate(String str, int maxWidth) 

Method Source Code

//package com.java2s;
/**// w w  w.  j  a v  a 2  s  . c  o  m
 * $Revision: 13635 $
 * $Date: 2013-05-04 15:55:48 +0800 (??, 04 ?? 2013) $
 *
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
 *
 * 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 {
    /**
     * Abbreviates a string to a specified length and then adds an ellipsis
     * if the input is greater than the maxWidth. Example input:
     * <pre>
     *      user1@jivesoftware.com/home
     * </pre>
     * and a maximum length of 20 characters, the abbreviate method will return:
     * <pre>
     *      user1@jivesoftware.c...
     * </pre>
     * @param str the String to abbreviate.
     * @param maxWidth the maximum size of the string, minus the ellipsis.
     * @return the abbreviated String, or <tt>null</tt> if the string was <tt>null</tt>.
     */
    public static String abbreviate(String str, int maxWidth) {
        if (null == str) {
            return null;
        }

        if (str.length() <= maxWidth) {
            return str;
        }

        return str.substring(0, maxWidth) + "...";
    }

    public static String substring(String subject, int start, int end) {
        if (subject != null && start < subject.length()) {
            return subject.substring(start, end);
        }
        return "";
    }

    public static String substring(String subject, int start) {
        if (subject != null && start < subject.length()) {
            return subject.substring(start);
        }
        return "";
    }
}

Related

  1. abbreviate(String str, int maximumChars)
  2. abbreviate(String str, int maxlen, String appendIfLimit)
  3. abbreviate(String str, int maxWidth)
  4. abbreviate(String str, int maxWidth)
  5. abbreviate(String str, int maxWidth)
  6. abbreviate(String str, int maxWidth)
  7. abbreviate(String str, int maxWidth)
  8. abbreviate(String str, int maxWidth)
  9. abbreviate(String str, int maxWidth)