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("
sbt 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));
}
}