Here you can find the source of abbreviate(String content, int maxWidth, String enc, String suffix)
public static String abbreviate(String content, int maxWidth, String enc, String suffix)
//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; } }