Java class characters crop tool

Is usually used in place we need to show the first name of Chinese characters, the interception of amplification, an effect similar to that Taobao recipient.
As shown below
Here Insert Picture Description

usage:

// Chinese character length determining taken
String name = "pleases";
// String calculating the byte length of the string, a character is two bytes
int = nameLenght TextCut.TextLength (name);
// here taken four byte, which is the first two characters,
String result = TextCut.getSubString (name,. 4);
of System.out.print (result); // print result is: is the
utility class below, copy and paste can be used directly .

public class TextCut {


    public static int TextLength(String value) {

        int valueLength = 0;

        String chinese = "[\u0391-\uFFE5]";

        /* 获取字段值的长度,如果含中文字符,则每个中文字符长度为2,否则为1 */

        for (int i = 0; i < value.length(); i++) {

            /* 获取一个字符 */

            String temp = value.substring(i, i + 1);

            /* 判断是否为中文字符 */

            if (temp.matches(chinese)) {

                /* 中文字符长度为2 */

                valueLength += 2;

            } else {

                /* 其他字符长度为1 */

                valueLength += 1;

            }

        }

        return valueLength;

    }


    public static String getSubString(String str, int length) {
        int count = 0;
        int offset = 0;
        char[] c = str.toCharArray();
        int size = c.length;
        if(size >= length){
            for (int i = 0; i < c.length; i++) {
                if (c[i] > 256) {
                    offset = 2;
                    count += 2;
                } else {
                    offset = 1;
                    count++;
                }
                if (count == length) {
                    return str.substring(0, i + 1);
                }
                if ((count == length + 1 && offset == 2)) {
                    return str.substring(0, i);
                }
            }
        }else{
            return str;
        }
        return "";
    }
}

Published 25 original articles · won praise 6 · views 9182

Guess you like

Origin blog.csdn.net/qq_38436214/article/details/96308962