去掉字符串后面所有的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)+$", "%"))
  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值