去掉字符串后面所有的0 去掉字符串前面或后面的0;

https://blog.csdn.net/weixin_43844810/article/details/94577879

去掉字符串前面或后面的0;

养猫还是养狗? 2019-07-03 17:10:23  4492  收藏 1
分类专栏: 工作
版权
 

方法一:

         int a = 0;
            //将油站编码前的0去掉
            if (jsonObject.getString("stationCode").substring(0, 1).equals("0")) {
       //转char数组
                char[] cx =jsonObject.getString("stationCode").toCharArray();
                String str = "";

        //把字符串每一个拿出来 拼接空格,在进行分割成数组
                for (int i = 0; i < cx.length; i++) {
                    str += cx[i];
                    str += " ";
                }
                String[] o = str.split(" ");
                for (int i = 0; i < o.length; i++) {
            //取到不为0时的位置
                    if (!o[i].equals("0")) {
                        a = i;
                        break;
                    }
                }
       //要去掉0的字符串        截取不为0的位置 jsonObject.put("stationCode",jsonObject.getString("stationCode").substring(a));
            }

方法二:

 jsonObject.put("stationCode", jsonObject.getString("stationCode").replaceFirst("^0*", ""));
 

去掉后面的0:

          String t = str.replaceAll("0+$", ""); 

成对去掉0 :

   String str=str.replaceAll("(00)+$", "%"))
  

猜你喜欢

转载自blog.csdn.net/qq_27327261/article/details/114544916