I'm trying to remove leading zeros from a string where there are multiple other strings and numbers in java
so ow - 00250 = 00000000 ]
for the above input I'm trying to get the output to be
so ow - 250 = 0 ]
I have tried
removeLeadingZeros(str.trim().replaceAll(" +", " ")
where removeLeadingZeros is
// Regex to remove leading
// zeros from a string
String regex = "^0+(?!$)";
// Replaces the matched
// value with given string
str = str.replaceAll(regex, "");
return str;
but I am getting the output
so ow - 00250 = 00000000 ]
which is the same as the original input.
These methods seem to only work for the first element in the string if it is a number such as
00015039 so ow + - 003948 83
which returns
15039 so ow + - 003948 83
but want to return
15039 so ow + - 3948 83
thanks!
By splitting the given string into array and then remove the leading zeroes, is working fine.
where removeLeadingZeros is:
public static String removeLeading(String str) {
String regex = "^0+(?!$)";
String x[] = str.split(" ");
StringBuilder z = new StringBuilder();
for(String y:x) {
str = y.replaceAll(regex, "");
z.append(" " + str);
}
return z.toString();
}