0

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.

James Kristen
  • 75
  • 1
  • 3
  • 12
  • Can you explain more detail about what you need? – Mostch Romi Aug 30 '18 at 03:50
  • Does your string only contain numbers? Or does it contain alphabetic characters as well? Update your question with the format of the input data and the expected output. – camickr Aug 30 '18 at 03:51
  • 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. – James Kristen Aug 30 '18 at 03:54
  • @JamesKristen don't forget to "accept" an answer by clicking on the check mark beside the solution you use so people know the problem has been solved. – camickr Aug 30 '18 at 04:18

2 Answers2

5

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
1

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360