首页 > 其他 > 详细

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

时间:2014-09-22 11:39:13      阅读:118      评论:0      收藏:0      [点我收藏+]
 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_IDEOGRAPHS
24                 || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
25                 || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
26                 || 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类型截取指定长度(指定长度=整数位+小数位)

原文:http://www.cnblogs.com/justbeginning/p/3985588.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!