Remove Leading Zeros From String in Java
Given a string of digits, remove leading zeros from it.
Input : 00000123569 Output : 123569
Input : 000012356090 Output : 12356090
- Count leading zeros by iterating string using charAt(i) and checking for 0 at the “i” th indices.
- Converting a string into StringBuffer object as strings are immutable
- Use StringBuffer replace function to remove characters equal to the above count using replace() method.
- Returning string after removing zeros
This article is contributed by Mr. Somesh Awasthi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.