【工具类】字符串处理工具类

去开头的“/”与结尾的“?1234”
应用大小的转换类
转换进度成百分比形式
去除path中开头的“/”
星级转换方法

View Code
  1 import java.text.DecimalFormat;
  2 
  3 public class StringUtil {
  4     /**
  5      * 
  6     * @Title: getAppPath
  7     * @Description: TODO   “/web/webdsfs/sfwe12re.apk?12324”--- 去开头的“/”与结尾的“?1234”
  8     * @param str
  9     * @return
 10      */
 11     public static String getAppPath(String str) {
 12         if(str == null){
 13             return null;
 14         }
 15         if (!str.contains("?") && !str.startsWith("/")) {
 16             return str;
 17         }
 18         int starPosition = 0;
 19         int endPosition;
 20         if (str.startsWith("/")) {
 21             starPosition = 1;
 22         }
 23         if (str.contains("?")) {
 24             endPosition = str.indexOf("?");
 25         } else {
 26             endPosition = str.length();
 27         }
 28         endPosition = str.length();
 29         return str.substring(starPosition, endPosition);
 30     }
 31 
 32     /**
 33      * 
 34      * @Title: getDataSize
 35      * @Description: TODO 每个Item中的应用大小的转换类
 36      * @param size
 37      * @return
 38      */
 39     public static String getDataSize(String sSize) {
 40         if (sSize == null || "".equals(sSize)) {
 41             return " 0 M";
 42         }
 43         long size = Long.parseLong(sSize);
 44         if (size < 0) {
 45             size = 0;
 46         }
 47         DecimalFormat formater = new DecimalFormat("####.00");
 48         if (size < 1024) {
 49             return size + "bytes";
 50         } else if (size < 1024 * 1024) {
 51             float kbsize = size / 1024f;
 52             return formater.format(kbsize) + "KB";
 53         } else if (size < 1024 * 1024 * 1024) {
 54             float mbsize = size / 1024f / 1024f;
 55             return formater.format(mbsize) + "MB";
 56         } else if (size < 1024 * 1024 * 1024 * 1024) {
 57             float gbsize = size / 1024f / 1024f / 1024f;
 58             return formater.format(gbsize) + "GB";
 59         } else {
 60             return " 0 M";
 61         }
 62     }
 63 
 64     /**
 65      * 
 66      * @Title: getProgress
 67      * @Description: TODO 转换进度成百分比形式
 68      * @param current
 69      *            当前下载大小
 70      * @param total
 71      *            当大小
 72      * @return
 73      */
 74     public static String getProgress(int current, int total) {
 75         if (total == 0) {
 76             return 0 + "%";
 77         }
 78         DecimalFormat formater = new DecimalFormat("##0.0");
 79         float kbsize = (float) (current) / (float) total * 100;
 80         if(kbsize > 100f){
 81             kbsize = 100f; 
 82         }
 83         return formater.format(kbsize) + "%";
 84     }
 85 
 86     /**
 87      * 
 88      * @Title: removeSpace
 89      * @Description: TODO 去除path中开头的“/”
 90      * @return
 91      */
 92     public static String removeSpace(String path) {
 93         if (!path.startsWith("/")) {
 94             return path;
 95         }
 96         String address = path.substring(1, path.length());
 97         return address;
 98     }
 99 
100     /**
101      * 星级转换方法
102      * @param star_level
103      * @return
104      */
105     public static String getStar_level(String star_level) {
106         if (star_level == null) {
107             return "0.0";
108         }
109         float star_num = Float.parseFloat(star_level);
110         DecimalFormat formater = new DecimalFormat("#0.0");
111         return formater.format(star_num);
112     }
113 }

 

 

 

posted on 2013-03-21 11:52  大米稀饭  阅读(170)  评论(0编辑  收藏  举报