Java String Abbreviate abbreviate(String src, int maxlen, String replacement)

Here you can find the source of abbreviate(String src, int maxlen, String replacement)

Description

abbreviate

License

Open Source License

Declaration

public static String abbreviate(String src, int maxlen, String replacement) 

Method Source Code

//package com.java2s;
/**//www .  ja v  a  2  s.  c om
 * Tencent is pleased to support the open source community by making Tars available.
 *
 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
 *
 * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * https://opensource.org/licenses/BSD-3-Clause
 *
 * 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 {
    public static String abbreviate(String src, int maxlen, String replacement) {

        if (src == null)
            return "";

        if (replacement == null) {
            replacement = "";
        }

        StringBuffer dest = new StringBuffer();

        try {
            maxlen = maxlen - computeDisplayLen(replacement);

            if (maxlen < 0) {
                return src;
            }

            int i = 0;
            for (; i < src.length() && maxlen > 0; ++i) {
                char c = src.charAt(i);
                if (c >= '\u0000' && c <= '\u00FF') {
                    maxlen = maxlen - 1;
                } else {
                    maxlen = maxlen - 2;
                }
                if (maxlen >= 0) {
                    dest.append(c);
                }
            }

            if (i < src.length() - 1) {
                dest.append(replacement);
            }
            return dest.toString();
        } catch (Throwable e) {
        }
        return src;
    }

    public static String abbreviate(String src, int maxlen) {
        return abbreviate(src, maxlen, "");
    }

    public static int computeDisplayLen(String s) {
        int len = 0;
        if (s == null) {
            return len;
        }

        for (int i = 0; i < s.length(); ++i) {
            char c = s.charAt(i);
            if (c >= '\u0000' && c <= '\u00FF') {
                len = len + 1;
            } else {
                len = len + 2;
            }
        }
        return len;
    }
}

Related

  1. abbreviate(String longStr, int maxLength)
  2. abbreviate(String name)
  3. abbreviate(String s, int length)
  4. abbreviate(String s, int max)
  5. abbreviate(String s, int maxLength)
  6. abbreviate(String str)
  7. abbreviate(String str, int maximumChars)
  8. abbreviate(String str, int maxlen, String appendIfLimit)
  9. abbreviate(String str, int maxWidth)