去掉字符串后面所有的0 去掉字符串前面或后面的0;
https://blog.csdn.net/weixin_43844810/article/details/94577879
?
?
去掉字符串前面或后面的0;
養(yǎng)貓還是養(yǎng)狗? 2019-07-03 17:10:23 ?4492 ?收藏 1
分類專欄: 工作
版權(quán)
?
方法一:
?????????int a = 0;
????????????//將油站編碼前的0去掉
????????????if (jsonObject.getString("stationCode").substring(0, 1).equals("0")) {
???????//轉(zhuǎn)char數(shù)組
????????????????char[] cx =jsonObject.getString("stationCode").toCharArray();
????????????????String str = "";
????????//把字符串每一個拿出來 拼接空格,在進行分割成數(shù)組
????????????????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)+$", "%"))
??
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的去掉字符串后面所有的0 去掉字符串前面或后面的0;的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 动视总裁 Rob Kostich 表态:
- 下一篇: 苹果初代 Apple Pencil 有望