使用正則表達式去掉字符串前面和后面多余的0


記錄一下小的工具代碼

1、去掉字符串前邊的0

String str = "000000021";
String newStr = str.replaceAll("^(0+)", "");
System.out.println(newStr);

2、去掉字符串后邊的0

String str = "2100000";
String newStr = str.replaceAll("0*$", "");
System.out.println(newStr);

兩種方法結果均為21


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM