James Kristen
James Kristen

Reputation: 85

How to remove leading zeros from Strings in java

I am aware that we can use String Utils library. But what if the String has only "0". It returns " ". Do we have any other way to remove leading 0 for all other strings except for "0".

Here is the scenario:

So, I am truncating the value of the Strings that are being entered by the users on the client side.All the inputs are taken in form of strings. For example if the user enters the department number as 009, I will truncate it to 9. For 08 it should be 8. But , here there is a department with 0. So when user is entering only 0, this method removes 0 too

Also what if the input is 000. I need last 0 with out being truncated.

Upvotes: 0

Views: 18189

Answers (2)

camickr
camickr

Reputation: 324098

I am aware that we can use String Utils library. But what if the String has only "0". It returns " ". Do we have any other way to remove leading 0 for all other strings except for "0".

You can create your own utility method that does exactly what you want.

Well you still haven't answered my question about whether the department can be alphanumeric or just numeric.

Based on your examples you could just convert the String to an Integer. The toString() implementation of an Integer removes leading zeroes:

    System.out.println( new Integer("008").toString() );
    System.out.println( new Integer("000").toString() );
    System.out.println( new Integer("111").toString() );

If the string can contain alphanumeric the logic would be more complex, which is why it is important to define the input completely.

For an alphanumeric string you could do something like:

StringBuilder sb = new StringBuilder("0000");

while (sb.charAt(0) == '0' && sb.length() > 1)
    sb.deleteCharAt(0);

System.out.println(sb);

Or, an even more efficient implementation would be something like:

int i = 0;

while (product.charAt(i) == '0' && i < product.length() - 1)
    i++;

System.out.println( product.substring(i) );

The above two solutions are the better choice since they will work for numeric and alphanumeric strings.

Or you could even use the StringUtils class to do what you want:

String result = StringUtils.removeleadingZeroes(...) // whatever the method is

if (result.equals(" "))
    result = "0";

return result;

In all solutions you would create a method that you pass parameter to and then return the String result.

Upvotes: 5

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520948

If you want to use a regex based approach, then one option would be to greedily remove all zeroes, starting from the beginning, so long as we do not replace the final character in the string. The following pattern does this:

^0+(?=.)

The lookahead ensures that there is at least one digit remaining, hence, a final zero will never be replaced.

String input1 = "040008";
String input2 = "000008";
String input3 = "000000";
input1 = input1.replaceAll("^0+(?=.)", "");
input2 = input2.replaceAll("^0+(?=.)", "");
input3 = input3.replaceAll("^0+(?=.)", "");
System.out.println(input1);
System.out.println(input2);
System.out.println(input3);

40008
8
0

Demo

By the way, I like the answer by @camickr and you should consider that as an option.

Upvotes: 2

Related Questions