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.