Java String Abbreviate abbrv(String str, int max)

Here you can find the source of abbrv(String str, int max)

Description

Abbreviate the given string.

License

Apache License

Parameter

Parameter Description
str a parameter
max a parameter

Declaration

public static String abbrv(String str, int max) 

Method Source Code

//package com.java2s;
/******************************************************************************
 *  Copyright 2015 by OLTPBenchmark Project                                   *
 *                                                                            *
 *  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 {
    /**/*from   ww  w  .  jav a  2 s . c o  m*/
     * Abbreviate the given string. The last three chars will be periods
     * @param str
     * @param max
     * @return
     */
    public static String abbrv(String str, int max) {
        return (abbrv(str, max, true));
    }

    /**
     * Abbreviate the given string. If dots, then the last three chars will be periods
     * @param str
     * @param max
     * @param dots
     * @return
     */
    public static String abbrv(String str, int max, boolean dots) {
        int len = str.length();
        String ret = null;
        if (len > max) {
            ret = (dots ? str.substring(0, max - 3) + "..." : str.substring(0, max));
        } else {
            ret = str;
        }
        return (ret);
    }
}

Related

  1. abbreviateScript(String script)
  2. abbreviateText(final String text, final int numberOfCharacters, final String appendText)
  3. abbreviateText(String text, int maxLength)
  4. abbreviation(String description)
  5. abbreviationtoLetter(String mutation)