Java String Abbreviate abbreviate(final String str, int lower, int upper, final String appendToEnd)

Here you can find the source of abbreviate(final String str, int lower, int upper, final String appendToEnd)

Description

see WordUtils#abbreviate.

License

GNU Affero Public License

Parameter

Parameter Description
str the str
lower the lower
upper the upper
appendToEnd the append to end

Return

the string

Declaration

public static String abbreviate(final String str, int lower, int upper, final String appendToEnd) 

Method Source Code

//package com.java2s;
/*//from   ww  w.j a  va2s  . c o  m
 *
 * Copyright (C) 2007-2015 Licensed to the Comunes Association (CA) under
 * one or more contributor license agreements (see COPYRIGHT for details).
 * The CA licenses this file to you under the GNU Affero General Public
 * License version 3, (the "License"); you may not use this file except in
 * compliance with the License. This file is part of kune.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

public class Main {
    /**
     * see WordUtils#abbreviate.
     *
     * @param str
     *          the str
     * @param lower
     *          the lower
     * @param upper
     *          the upper
     * @param appendToEnd
     *          the append to end
     * @return the string
     */
    public static String abbreviate(final String str, int lower, int upper, final String appendToEnd) {
        // initial parameter checks
        if (str == null) {
            return null;
        }
        if (str.length() == 0) {
            return "";
        }

        // if the lower value is greater than the length of the string,
        // set to the length of the string
        if (lower > str.length()) {
            lower = str.length();
        }
        // if the upper value is -1 (i.e. no limit) or is greater
        // than the length of the string, set to the length of the string
        if (upper == -1 || upper > str.length()) {
            upper = str.length();
        }
        // if upper is less than lower, raise it to lower
        if (upper < lower) {
            upper = lower;
        }

        final StringBuffer result = new StringBuffer();
        final int index = str.indexOf(" ", lower);
        if (index == -1) {
            result.append(str.substring(0, upper));
            // only if abbreviation has occured do we append the appendToEnd value
            if (upper != str.length()) {
                result.append(defaultString(appendToEnd));
            }
        } else if (index > upper) {
            result.append(str.substring(0, upper));
            result.append(defaultString(appendToEnd));
        } else {
            result.append(str.substring(0, index));
            result.append(defaultString(appendToEnd));
        }
        return result.toString();
    }

    /**
     * Default string.
     *
     * @param str
     *          the str
     * @return the string
     */
    private static String defaultString(final String str) {
        return str == null ? "" : str;
    }
}

Related

  1. abbr(String str, int length)
  2. abbrev(String longString, int size)
  3. abbrev(String result, int maxLength, String separator)
  4. abbreviate(final String str)
  5. abbreviate(final String str, final int maxWidth)
  6. abbreviate(final String value, final int maxLength)
  7. abbreviate(final String value, int max)
  8. abbreviate(String _str, int _length)
  9. abbreviate(String content, int maxWidth, String enc, String suffix)