Regex is the best tool for the job; what it should be depends on the problem specification. The following removes leading zeroes, but leaves one if necessary (i.e. it wouldn't just turn "0" to a blank string).
s.replaceFirst("^0+(?!$)", "")
The ^ anchor will make sure that the 0+ being matched is at the beginning of the input. The (?!$) negative lookahead ensures that not the entire string will be matched.
Test harness:
String[] in = {
"01234", // "[1234]"
"0001234a", // "[1234a]"
"101234", // "[101234]"
"000002829839", // "[2829839]"
"0", // "[0]"
"0000000", // "[0]"
"0000009", // "[9]"
"000000z", // "[z]"
"000000.z", // "[.z]"
};
for (String s : in) {
System.out.println("[" + s.replaceFirst("^0+(?!$)", "") + "]");
}
See also
- regular-expressions.info
- repetitions, lookarounds, and anchors
String.replaceFirst(String regex)
Regex is the best tool for the job; what it should be depends on the problem specification. The following removes leading zeroes, but leaves one if necessary (i.e. it wouldn't just turn "0" to a blank string).
s.replaceFirst("^0+(?!$)", "")
The ^ anchor will make sure that the 0+ being matched is at the beginning of the input. The (?!$) negative lookahead ensures that not the entire string will be matched.
Test harness:
String[] in = {
"01234", // "[1234]"
"0001234a", // "[1234a]"
"101234", // "[101234]"
"000002829839", // "[2829839]"
"0", // "[0]"
"0000000", // "[0]"
"0000009", // "[9]"
"000000z", // "[z]"
"000000.z", // "[.z]"
};
for (String s : in) {
System.out.println("[" + s.replaceFirst("^0+(?!$)", "") + "]");
}
See also
- regular-expressions.info
- repetitions, lookarounds, and anchors
String.replaceFirst(String regex)
You can use the StringUtils class from Apache Commons Lang like this:
StringUtils.stripStart(yourString,"0");
If the string always contains a valid integer the return new Integer(value).toString(); is the easiest.
public static String removeLeadingZeroes(String value) {
return new Integer(value).toString();
}
- Stop reinventing the wheel. Almost no software development problem you ever encounter will be the first time it has been encountered; instead, it will only be the first time you encounter it.
- Almost every utility method you will ever need has already been written by the Apache project and/or the guava project (or some similar that I have not encountered).
- Read the Apache StringUtils JavaDoc page. This utility is likely to already provide every string manipulation functionality you will ever need.
Some example code to solve your problem:
public String stripLeadingZeros(final String data)
{
final String strippedData;
strippedData = StringUtils.stripStart(data, "0");
return StringUtils.defaultString(strippedData, "0");
}
No, the correct regular expression is something more complex, and it needs positive look-behind.
str = str.replaceAll("\\.0*$|(?<=\\.[0-9]{0,2147483646})0*$", "");
you have to escape the ., because otherwise it means "any character", you have to anchor the regex only to the end of the string, and you have to say that the 0 digits must be after a . plus some optional non-zero digits.
Addendum: there is a "special case" that should be handled: 1.00. We handle it by using the |. The first sub-expression means "a dot plus only zeroes" and matches even the dot (that in this way is deleted)
And remember that in Java strings are immutable, so replaceAll will create a new string.
Note the use of {0,2147483646}: if you used a * you would get a Look-behind group does not have an obvious maximum length, because the * would be converted to {0,2147483647} and considering the "+1" length of the \\. it would overflow, so we put the maximum number of digits possible (2147483647, maximum value of a signed int) minus 1 for the dot.
Test example: http://ideone.com/0NDTSq
Since you want rounding of the numbers, you should not use regular expressions. You should use DecimalFormat:
DecimalFormat twoDForm = new DecimalFormat("#.#");
Double d = Double.parseDouble("123.078");
String s = twoDForm.format(d);
System.out.println(s);
will output:
123.08
there are possibilities:
1000 -> 1000
10.000 -> 10 (without point in result)
10.0100 -> 10.01
10.1234 -> 10.1234
I am lazy and stupid, just
s = s.indexOf(".") < 0 ? s : s.replaceAll("0*$", "").replaceAll("\\.$", "");
Same solution using contains instead of indexOf as mentioned in some of the comments for easy understanding
s = s.contains(".") ? s.replaceAll("0*$","").replaceAll("\\.$","") : s
Use DecimalFormat, its cleanest way
String s = "10.1200";
DecimalFormat decimalFormat = new DecimalFormat("0.#####");
String result = decimalFormat.format(Double.valueOf(s));
System.out.println(result);