首页 > 代码库 > [1]字符串按中文符占3位进行指定长度剪切[2]Double类型截取指定长度(指定长度=整数位+小数位)

[1]字符串按中文符占3位进行指定长度剪切[2]Double类型截取指定长度(指定长度=整数位+小数位)

 1     /** 将中文字符串剪切为在当前db2(编码GBK)中所占用的长度*/ 2     public String cutStringForDb2(String src,Integer size) { 3         int len = src.length(); 4         int res_len = 0; 5         StringBuilder res_b = new StringBuilder(); 6         for (int i = 0 ; i < len; i++) { 7             if (isChinese(src.charAt(i))) { 8                 res_len+=3; 9             } else {10                 res_len+=1;11             }12             if (res_len>size) {13                 break;14             } else {15                 res_b.append(src.charAt(i));16             }17         }18         return res_b.toString();19     }20     21     public boolean isChinese(char c) {22         Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);23         if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS24                 || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B25                 || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS26                 || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {27             return true;28         }29         return false;30     }31     32     public Double cutDouble(String src,Integer intValue,Integer pointValue) {33         if (src.length() > intValue) {34             if (src.matches("[0-9]*\\.[0-9]*")) {35                 String[] strs = src.split("\\.");36                 StringBuilder res = new StringBuilder().append(strs[0].length()>intValue?37                         strs[0].substring(0, intValue):strs[0]);38                 res.append(strs[1].length()>pointValue?strs[1].substring(0, pointValue):strs[1]);39                 return Double.parseDouble(res.toString());40             } else if (src.matches("[0-9]*")){41                 return Double.parseDouble(src.substring(0, intValue));42             } else {43                 return 0.0D;44             }45         } else {46             return Double.parseDouble(src);47         }48     }

 

[1]字符串按中文符占3位进行指定长度剪切[2]Double类型截取指定长度(指定长度=整数位+小数位)