package com.common;
/**
* 截取字符串的长度
* @author S.J
* @version 1.0, 11/03/2009
*/
public class CharactorLength {
public final static int LENGTH_COMMON = 60;
public final static int LENGTH_SHORT = 40;
public final static int LENGTH_ICONS_VIEW = 20;
/**
* 判断一个字符是Ascill字符还是其它字符(如汉,日,韩文字符)
* @param char c, 需要判断的字符
* @return boolean, 返回true,Ascill字符
*/
public static boolean isLetter(char c) {
int k = 0x80;
return c / k == 0 ? true : false;
}
/**
* 得到一个字符串的长度,显示的长度,一个汉字或日韩文长度为2,英文字符长度为1
* @param String s, 需要得到长度的字符串
* @return int, 得到的字符串长度
*/
public static int length(String s) {
if (s == null)
return 0;
char[] c = s.toCharArray();
int len = 0;
for (int i = 0; i < c.length; i++) {
len++;
System.out.println("compare " + c[i] + " is letter? ");
if (!isLetter(c[i])) {
len++;
System.out.print("No");
}
System.out.println("");
}
return len;
}
/**
* 截取一段字符的长度,不区分中英文,如果数字不正好,则少取一个字符位
* @author patriotlml
* @param String origin, 原始字符串
* @param int len, 截取长度(一个汉字长度按2算的)
* @return String, 返回的字符串
*/
public static String substring(String originString, int len) {
System.out.println("substring start----------------");
String origin = null;
try{
origin = originString;//StringConverter.asciiToUnicode(originString);
System.out.println(origin);
System.out.println("ISO-8859-1-UTF8 "+new String(originString.getBytes("ISO-8859-1"), "UTF-8"));
System.out.println("ISO-8859-1-GBK "+new String(originString.getBytes("ISO-8859-1"), "GBK"));
System.out.println("ISO-8859-1-GB2312 "+new String(originString.getBytes("ISO-8859-1"), "GB2312"));
System.out.println("ISO-8859-1-UNICODE "+new String(originString.getBytes("ISO-8859-1"), "UNICODE"));
System.out.println("UNICODE-UTF8 "+new String(originString.getBytes("UNICODE"), "UTF-8"));
System.out.println("UNICODE-GBK "+new String(originString.getBytes("UNICODE"), "GBK"));
System.out.println("UNICODE-GB2312 "+new String(originString.getBytes("UNICODE"), "GB2312"));
System.out.println("UNICODE-ISO-8859-1 "+new String(originString.getBytes("UNICODE"), "ISO-8859-1"));
System.out.println("UTF8-GBK "+new String(originString.getBytes("UTF-8"), "GBK"));
System.out.println("UTF8-GB2312 "+new String(originString.getBytes("UTF-8"), "GB2312"));
System.out.println("UTF8-UNICODE "+new String(originString.getBytes("UTF-8"), "UNICODE"));
System.out.println("UTF8-ISO-8859-1 "+new String(originString.getBytes("UTF-8"), "ISO-8859-1"));
System.out.println("GBK-UTF8 "+new String(originString.getBytes("GBK"), "UTF-8"));
System.out.println("GBK-UNICODE "+new String(originString.getBytes("GBK"), "UNICODE"));
System.out.println("GBK-ISO-8859-1 "+new String(originString.getBytes("GBK"), "ISO-8859-1"));
System.out.println("GBK-GB2312 "+new String(originString.getBytes("GBK"), "GB2312"));
System.out.println("GB2312-UTF8 "+new String(originString.getBytes("GB2312"), "UTF-8"));
System.out.println("GB2312-UNICODE "+new String(originString.getBytes("GB2312"), "UNICODE"));
System.out.println("GB2312-ISO-8859-1 "+new String(originString.getBytes("GB2312"), "ISO-8859-1"));
System.out.println("GB2312-GBK "+new String(originString.getBytes("GB2312"), "GBK"));
}catch(Exception e){
e.printStackTrace();
}
if (origin == null || origin.equals("") || len < 1)
return "";
byte[] sbt = originString.getBytes();
System.out.println("sbt start:::::::::::::::::::::::::");
for(int i=0; i<sbt.length; i++){
System.out.print(sbt[i]);
}
System.out.println("\nsbt end:::::::::::::::::::::::::");
byte[] strByte = new byte[len];
if (len >= length(origin)){
return origin;
}
System.out.println("strByte.length=" + strByte.length);
strByte = origin.substring(0, len).getBytes();
// System.arraycopy(origin , 0, strByte, 0, len);
int count = 0;
for (int i = 0; i < len; i++) {
int value = (int) strByte[i];
System.out.println("strByte_" + i + strByte[i]);
if (value < 0) {
count++;
}
}
System.out.println("count=" + count);
if (count % 2 != 0) {
len = (len == 1) ? ++len : --len;
}
System.out.println("len=" + len);
return new String(strByte, 0, len) + "...";
}
public static void main(String[] args) {
String str = "-56-3-54-65-51-88-50-64-54-65-51-88-50-64-54-65-51-88-50-64-54-65-51-88-50-64-54-65-51-88-50-64-54-65-51-88-50-64-54-65-51-88-50-64-54-65-51-88-50-64-54-65-51-88-50-64-54-65-51-88-50-64-54-65-51-88-50-64-54-65-51-88-50-64-54-65-51-88-50-64-54-65-51-88-50-64-54-65-51-88-50-64-54-65-51-88-50-64-51-88";
String str_cn = "测试中文字符长度测试中文字符长度测试中文字符长度测试中文字符长度测试中文字符长度测试中文字符长度测试中文字符长度";
System.out.println(substring(str, LENGTH_SHORT));
System.out.println(substring(str_cn, LENGTH_SHORT));
}
}