java - How to remove leading zeros from numeric text? -
i using remove leading zeros input string.
return a.replaceall("^0+","");
but above string removes string alphanumeric well. not want that. requirement is:
only leading zeros of numeric numbers should removed e.g.
00002827393 -> 2827393
if have alpha numeric number leading zeros must not removed e.g.
000zz12340 -> 000zz12340
you can check if string composed of digits first :
pattern p = pattern.compile("^\\d+$"); matcher m = p.matcher(a); if(m.matches()){ return a.replaceall("^0+", ""); } else { return a; }
also :
- consider making pattern static, way created once
- you may want isolate part matcher in smaller function, "isonlydigit" example
Comments
Post a Comment