
/**
* 按照指定長度將字符串進行分割,中文字符算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 閱讀(2145)
評論(1) 編輯 收藏 所屬分類:
JAVA