Explain Codes LogoExplain Codes Logo

How to remove leading zeros from alphanumeric text?

java
regex-patterns
string-manipulation
java-methods
Nikita BarsukovbyNikita Barsukov·Dec 2, 2024
TLDR
String result = "0042Example".replaceAll("^0+", ""); System.out.println(result); // "42Example"

By using the replaceAll method and a regex pattern ^0+, we can remove leading zeros from alphanumeric text in Java. This targets the beginning of the string with one or more zeros (0+). Result is the original string minus any leading zeros.

Delving into regex: patterns, matches, and laughs

If you are someone who enjoys fishing for solutions in the vast sea of programming, regex can be your most reliable fishing net. Here's how you can maneuver this net using different regex patterns.

The stuff after the caret matters, a lot

Ever heard about a caret ^? No, it's not the kind superheroes wear. It's an anchor that designates the beginning of a line in regex. Couple it with 0* and you got yourself a fantastic pattern that says "Match any number of zeros at the beginning".

String result = "000SomeValue".replaceAll("^0*", "");//we're not talking about Harry Potter's wand here

Remember, the goal is to remove the zeros, not add or turn anyone into frogs.

Use replaceFirst but be cautious

Although replaceFirst is a Java method that utilizes regex, you need to tread carefully. It removes leading zeros but you might end up with an empty string, especially when your value is composed solely of zeros. So, an additional check is in order:

String zerosOnly = "0000".replaceFirst("^0+", ""); System.out.println(zerosOnly.isEmpty() ? "0" : zerosOnly); // "0"

Apache strings to the rescue

Regex is fantastic but some find it intimidating (see what I did there? No? You'll find it funny someday). You can use the StringUtils.stripStart method from Apache Commons Lang for an easy, non-regex solution to remove leading zeros:

String resultNonRegex = StringUtils.stripStart("0042Example", "0"); System.out.println(resultNonRegex); // "42Example"

You just have to make sure Apache is always by your side (just like in the Wild West).

Edge cases, because Strings are unpredictable

The universe of strings is as vast and diverse as our own. You can encounter strings with multiple zeros, hyphens, or strings that are solely zeros.

Watch out for hyphenated values

In the case of hyphenated alphanumeric text, add a negative lookbehind in your regex to avoid stripping zeros following hyphens. Here's the magic formula:

String resultWithHyphens = "00-420-Example-00".replaceAll("(?<!-)0+", ""); System.out.println(resultWithHyphens); // "-420-Example-00"

Because everybody knows that zeros after hyphens matter (just ask -008).

Enter the trim-level-zero master function

In some situations, when regex doesn't cut it, crafting a custom function can give you ultimate control over how you want to strip those annoying leading zeros:

public static String trimLeadingZeros(String input) { int i = 0; while (i < input.length() && input.charAt(i) == '0') { i++; } return i == input.length() ? "0" : input.substring(i); }

This function is like a scissors that trims the zeros at just the right place.

Stepping into Kotlin's territory

For those who dabble in Kotlin, there's a built-in method called trimStart that trims the character specified:

val resultKotlin = "0042Example".trimStart('0') println(resultKotlin) // "42Example"

Think of it as Java's stripStart method, but in a Kotlin-y way.