Java String Abbreviate abbreviate(String content, int maxWidth, String enc, String suffix)

Here you can find the source of abbreviate(String content, int maxWidth, String enc, String suffix)

Description

abbreviate

License

Apache License

Declaration

public static String abbreviate(String content, int maxWidth, String enc, String suffix) 

Method Source Code


//package com.java2s;
/*/*from  www . j a v a 2 s. c  o m*/
 * Copyright 2001-2006 The Apache Software Foundation.
 *
 * 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.
 */

import java.io.UnsupportedEncodingException;

public class Main {

    public static String abbreviate(String content, int maxWidth, String enc, String suffix) {
        if (content == null)
            return "";
        if (maxWidth < suffix.length())
            throw new IllegalArgumentException();
        int ptr = maxWidth - suffix.length();
        String str = null;
        try {
            byte bytes[] = content.getBytes(enc);
            str = new String(bytes, 0, bytes.length >= ptr ? ptr : bytes.length, enc);
        } catch (UnsupportedEncodingException e) {
            //throw new Exception();
            return "";
        }
        for (ptr = (ptr = str.length() - 4) >= 0 ? ptr : 0; ptr < str.length()
                && str.charAt(ptr) == content.charAt(ptr); ptr++)
            ;
        return str.substring(0, ptr) + suffix;
    }
}

Related

  1. abbreviate(final String str, final int maxWidth)
  2. abbreviate(final String str, int lower, int upper, final String appendToEnd)
  3. abbreviate(final String value, final int maxLength)
  4. abbreviate(final String value, int max)
  5. abbreviate(String _str, int _length)
  6. abbreviate(String fileName, int maxLen)
  7. abbreviate(String input, int length)
  8. abbreviate(String longName)
  9. abbreviate(String longStr, int maxLength)