/** 将中文字符串剪切为在当前db2(编码GBK)中所占用的长度*/
public String cutStringForDb2(String src,Integer size) {
int len = src.length();
int res_len = 0;
StringBuilder res_b = new StringBuilder();
for (int i = 0 ; i < len; i++) {
if (isChinese(src.charAt(i))) {
res_len+=3;
} else {
res_len+=1;
}
if (res_len>size) {
break;
} else {
res_b.append(src.charAt(i));
}
}
return res_b.toString();
} public boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
return true;
}
return false;
} public Double cutDouble(String src,Integer intValue,Integer pointValue) {
if (src.length() > intValue) {
if (src.matches("[0-9]*\\.[0-9]*")) {
String[] strs = src.split("\\.");
StringBuilder res = new StringBuilder().append(strs[0].length()>intValue?
strs[0].substring(0, intValue):strs[0]);
res.append(strs[1].length()>pointValue?strs[1].substring(0, pointValue):strs[1]);
return Double.parseDouble(res.toString());
} else if (src.matches("[0-9]*")){
return Double.parseDouble(src.substring(0, intValue));
} else {
return 0.0D;
}
} else {
return Double.parseDouble(src);
}
}