
/**
* 按照指定长度将字符串进行分割,中文字符算2个长度
* @param str 字符串
* @param length 指定长度
* @return 如果字符串长度超出指定长度
* ,则将字符串分成2个部分,分别装在map中
*/

public static Map getStr(String str, int length) {
HashMap hashMap = new HashMap();
String addr1 = "";
String addr2 = "";

byte tmpBytes[] = str.getBytes();
int iByteLen = tmpBytes.length;

if (iByteLen > length) {
int iLen = 0;

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

if ((tmpBytes[i] & 0xFF) > 0x80) {
iLen += 2;
i++;
continue;

} else {
iLen += 1;
continue;
}
}

addr1 = new String(tmpBytes, 0, iLen);
addr2 = new String(tmpBytes, iLen, iByteLen - iLen);

} else {
addr1 = str;
}
hashMap.put(new Integer(1), addr1);
hashMap.put(new Integer(2), addr2);
return hashMap;
}

0x80等于十进制的128,Turbo C中规定对ASCII码值大于0x80的字符将被认为是负数。
posted on 2009-09-22 22:32
xrzp 阅读(2141)
评论(1) 编辑 收藏 所属分类:
JAVA