Shorten Chinese String - Android Internationalization

Android examples for Internationalization:Chinese

Description

Shorten Chinese String

Demo Code

import android.util.Log;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

public class Main{

    public static String ShortenCn(String str, int len) {
        return ShortenCn(str, len, "...", 2);
    }//www. java2s  .co  m

    public static String ShortenCn(String str, int len, String suffix,
            int suffLen) {
        if ("".equals(str) || str == null || str.trim().equals(""))
            return "";
        if (suffix.length() >= str.length())
            suffix = "";

        StringBuffer sb = new StringBuffer();
        int counter = 0;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            sb.append(c);
            if (c < 255) {
                counter++;
            } else {
                counter = counter + 2;
            }
            if (counter > len - suffLen) {
                if (i < str.length() - 1) {
                    sb.delete(sb.length() - 1, sb.length());
                    sb.append(suffix);
                }
                break;
            }
        }
        return sb.toString();
    }

}

Related Tutorials