Reputation: 55
Given:
a = 00099999325
b = 1254
How do I remove leading zeros but leaves one necessary
This is the output I'm expecting:
099999325
1254
Currently, am using this function but i need to optimize it :
//removes leading zeroes, but leaves one if necessary
public static String removeLeadingZeroes(String s) {
return s.replaceFirst("^0+(?!$)", "");
}
can anyone help me please ? thank you for advanced
Upvotes: 0
Views: 65
Reputation: 59950
You can use this regex ^0*(0\d+)
like this:
return s.replaceFirst("^0*(0\\d+)", "$1");
I/O
000099999325 -> 099999325
1254 -> 1254
1230004 -> 1230004
Upvotes: 1