给定一个数字串,去掉前导零。
null
例如:
Input : 00000123569 Output : 123569 Input : 000012356090 Output : 12356090
1) 数一数前导零。 2) 使用StringBuffer replace函数删除大于计数的字符。
// Java program to remove leading/preceding zeros // from a given string import java.util.Arrays; import java.util.List; /* Name of the class to remove leading/preceding zeros */ class RemoveZero { public static String removeZero(String str) { // Count leading zeros int i = 0 ; while (i < str.length() && str.charAt(i) == '0' ) i++; // Convert str into StringBuffer as Strings // are immutable. StringBuffer sb = new StringBuffer(str); // The StringBuffer replace function removes // i characters from given index (0 here) sb.replace( 0 , i, "" ); return sb.toString(); // return in String } // Driver code public static void main (String[] args) { String str = "00000123569" ; str = removeZero(str); System.out.println(str); } } |
输出:
123569
本文由 Somesh Awasthi先生 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END