Java Regular Expression Remove Leading Zeros Example

There are several ways in which leading zeros from String can be removed. Using Java Regular Expressions is one of them.

How to remove leading zeros from String using Java Regular Expressions?

Example String object

And the desired output is “51700”. Note that, we want to remove only leading zeros not the trailing one.

In order to achieve the desired output, regular expression can be built like,

Where ^ tells to search from start of the string and 0+ is for one or more zeros. Basically we want to replace all the zeros from start of the string with empty string value thus removing them. Let’s test our newly built expression.


Nice, it removed all the zeros from our input String. How about giving it a second run with different input value? This time input value is “000000” and desired output value is “0”.


Oops, that is not what we wanted. We wanted 0 as output if string contained all the zeros. Let’s change our regular expression a bit. This time we are going to search zeros at the start of the string but we want to make sure that if the string contains only zeros than it should return one zero.

The first part “^0+” of expression is still same. We want to match one or more zeros from start of the string. While the second part “(?!$)” is a negative lookahead expression where “$” means end of string.

In general terms, Lookahead is like if condition. Syntax of lookahead is (?expression). “!” is used as “not” operator (negative). So together “(?!expression)” forms negative lookahead.

In our example, expression “^0+(?!$)” means match one or more zeros if it is not followed by end of string. All characters in our string matches with the expression till lookahead condition is being satisfied. Last zero of our string will not match because it is followed by end of the string thus it will be retained.


This expression solves our original problem. Make sure to trim the leading and trailing spaces first from the string using trim() method. Check out various ways to remove leading and trailing space from String example.

If you don’t want to use negative lookahead, it can be addressed by adding one if statement in our original pattern like given below.

How to remove leading zeros from String without using Java Regular Expressions?

There are several ways we can do that.

1. Using indexOf() and substring() methods of String class as given in below example


If the first character of the string is zero, while loop’s condition becomes true and substring method will be executed. sourceString.substring(1) method creates substring of the original string starting from index 1 (thus removing first 0) and assigns it back to the original string. While loop continues to execute until it finds non-zero character as first character of the string thus removing all leading zeros from the string. Here is the String indexOf example and String substring example for reference.

2) Using chartAt() method of String class

It loops through the source string until it finds the first non-zero character in the string using charAt() method, get the substring of the original string from that index and breaks the loop.

This method is more efficient than the indexOf()/substring() approach given above because substring is taken only once when it finds non-zero character. The indexOf/substring() approach takes substring once for each leading zero found in the string.

3) Using parseInt() method of Integer wrapper class

One more simple solution is to use Integer wrapper class.

Note: This approach does not work if source string contains alphanumeric characters or if the string is empty. You can visit Java String to Int example & int to String example for more details.

4) Using Apache Commons

This is the easiest approach among all others, provided if you can use third-party library. Apache commons StringUtils class provides a very handy method stripStart which can be used to remove leading zeros from the source string as given below.

If you think there is a better approach to remove leading zeros from the string which is not covered here, Please let us know in the comments section below.

Refer to Java String Examples and Java Regular Expression Examples to explore more.

About the author



rahimv has over 15 years of experience in designing and developing Java applications. His areas of expertise are J2EE and eCommerce. If you like the website, follow him on Facebook, Twitter or Google Plus.

Add Comment

Your email address will not be published. Required fields are marked *