Java一些常用得转换

1提取字符串中得数字

   public static String getNumeric(String str) {
        String regEx="[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }

2 html中图片转换,让剧中显示

 public static String getFormatHtml(String content) {
        if (!content.contains("");
        strBuilder.append("");
        strBuilder.append("");
        strBuilder.append("无标题文档");
        strBuilder.append("");
        strBuilder.append("");
        strBuilder.append("");
        strBuilder.append("");
        strBuilder.append("
"); strBuilder.append(content.replaceAll("style=", "")); //此处为去掉原始属性。如果想去掉指定标签的style属性,此处需要特殊处理。 strBuilder.append("
"); strBuilder.append(""); strBuilder.append(""); return strBuilder.toString(); }

3 获取assets文件下json文件并转换成json

  String jsonstr = CommonUtil.getJson(mContext, "alladdress.json");
 public static String getJson(Context context, String fileName) {

        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = context.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }


4去除字符串末尾多余都0

StringUtils.trimZero(item.getPrice())) 3.00-->3  2.50->2.5
public static String trimZero(String s) {
        if (s.indexOf(".") > 0) {
            // 去掉多余的0
            s = s.replaceAll("0+?$", "");
            // 如最后一位是.则去掉
            s = s.replaceAll("[.]$", "");
        }
        return s;
    }

你可能感兴趣的:(Java一些常用得转换)