• [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_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     }
  • 相关阅读:
    Jquery操作select
    Session在类库中的使用
    从一个表取数据更新另一个表的信息
    判断数据库表中是否存在某个字段
    .net项目中上传的图片或者文件太大 无法上传
    计算机中丢失 msvcr110.dlll
    Ubuntu搭建FTP server
    Linux常用命令集
    系统清理篇
    ubuntu安装 ssh server
  • 原文地址:https://www.cnblogs.com/justbeginning/p/3985588.html
Copyright © 2020-2023  润新知