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)
Answer from polygenelubricants on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java string › remove leading and trailing characters from a string
Remove Leading and Trailing Characters from a String | Baeldung
January 8, 2024 - Note, that these methods accept a String as their second parameter. This String represents a set of characters, not a sequence we want to remove. For example, if we pass “01”, they’ll remove any leading or trailing characters, that are either ‘0’ or ‘1’.
🌐
GeeksforGeeks
geeksforgeeks.org › java › remove-leading-zeros-from-string-in-java
Remove Leading Zeros From String in Java - GeeksforGeeks
August 24, 2026 - Traverse the string while the current character is '0'. Stop when the first non-zero character is found. Return the substring starting from that index. If all characters are zeros, return "0".
🌐
CodeSpeedy
codespeedy.com › home › how to remove leading zeros from a string in java
How to remove leading zeros from a string in Java - CodeSpeedy
November 4, 2021 - package javaapplication16; import java.util.Arrays; public class JavaApplication16 { public static void main(String[] args) { String str = "000088469822"; int l = 0; char[] array = str.toCharArray(); l = array.length; int firstNonZeroAt = 0; for(int i=0; i<l; i++) { if(!String.valueOf(array[i]).equalsIgnoreCase("0")) { firstNonZeroAt = i; break; } } char [] newArray = Arrays.copyOfRange(array, firstNonZeroAt,l); String resultString = new String(newArray); System.out.println(resultString); } }
🌐
CopyProgramming
copyprogramming.com › howto › remove-leading-zeros-from-string-in-java
Java String: Complete Guide to Removing Leading and Trailing Zeros
January 3, 2026 - This guide covers modern techniques using both core Java and popular libraries, along with the latest best practices and performance considerations for 2026. Direct answer: The regex pattern ^0+(?!$) efficiently removes leading zeros while preserving a single zero if the entire string consists ...
🌐
Baeldung
baeldung.com › home › java › java string › remove insignificant zeros from a number represented as a string
Remove Insignificant Zeros From a Number Represented as a String | Baeldung
June 14, 2025 - Firstly, we’ll check if the String contains a dot character; we’ll recursively call the replaceAll() method to remove leading and trailing zeros from the input.
🌐
Prutor
prutor.ai › prutor online academy › java › remove leading zeros from string in java
Remove Leading Zeros From String in Java
/* Name of the class to remove leading/preceding zeros */ class RemoveZero { public static String removeZero(String str) { // Count leading zeros int i = 0; while (i < str.length() && str.charAt(i) == '0') i++; // Convert str into StringBuffer ...
🌐
Coderanch
coderanch.com › t › 656218 › java › removing-trailing-leading-zeroes-regex
Need help in removing trailing and leading zeroes using regex (Java in General forum at Coderanch)
October 5, 2015 - I don't know if we still are supposed to post hints instead of full solutions since this is now in Java in General, but I will say that I have a one-line solution that I will post if I get the go-ahead. Until then, think about using the method replaceFirst(). It's a little confusing because of the "first" in the name, but it's a way to use a regex to replace something in a string ("first" because it replaces only the first instance of it, but we don't care).
Find elsewhere
🌐
Java Code Geeks
javacodegeeks.com › home
Remove Insignificant Zeros From a Numeric String Example - Java Code Geeks
February 17, 2025 - Line 10: remove any leading zeros with the regular expression pattern: LEADING_ZERO_REGEX. Line 13: remove the trailing zeros after the decimal point. Line 20: handles the special case when the rawNumStr argument only contains zeros.
🌐
SAP Help Portal
help.sap.com › docs › SUPPORT_CONTENT › java › 3354613040.html
Remove Leading and Trailing Zeros from a String | SAP Help Portal
JAVA Code to convert date-time to any specific String format · Secure JavaMail With SSL · Simplify Log4J Using Property Configurator · Alphanumeric Random Key Generation · Properties file · Java Management Extension (JMX) Database access · Remove Leading and Trailing Zeros from a String ·
🌐
TutorialsPoint
tutorialspoint.com › remove-leading-zeroes-from-a-string-in-java-using-regular-expressions
Explain how to remove Leading Zeroes from a String in Java
October 15, 2019 - Following Java program reads an integer value from the user into a String and removes the leading zeroes from it using the stripStart() method of the StringUtils class.
🌐
TheLinuxCode
thelinuxcode.com › home › remove leading zeros from a string in java (with real-world edge cases and 2026-ready patterns)
Remove Leading Zeros From a String in Java (With Real-World Edge Cases and 2026-Ready Patterns) – TheLinuxCode
January 13, 2026 - If you truly expect signed numbers, treat the sign separately: parse the sign, strip zeros from the digits only, then reapply the sign. For identifiers, I usually reject signs to avoid ambiguous input. ... Java’s built-in string trimming utilities remove whitespace, not digits. There isn’t a built-in for “strip leading zeros,” so a custom utility is still the clearest approach.
🌐
Coderanch
coderanch.com › t › 590739 › java › Remove-leading-zeros-xx
Remove leading zeros up to 0.xx [Solved] (Java in General forum at Coderanch)
The strings will always end in a decimal place and two digits. So: 000756.90 becomes 756.90, and 0000070.50 should become 70.50 It seems like I want to do two things: 1. replace all instances of "00." with "0.", recursively, in case it's all leading zeros, and 2.
🌐
Stack Overflow
stackoverflow.com › a › 56457560 › 3832970
java - Remove leading Zeros from Decimal String - Stack Overflow
Copy@Test public void testStripLeadingZero() { List<String> tests = new ArrayList<>(Arrays.asList( "0.05", "-0.03", "100.03", "-100.03", "000010", "-00020", "0077.778", "-0088.888" )); for (String s : tests) { String result = s.replaceFirst("^0+", "").replaceFirst("^-0+", "-"); System.err.println("result: " + result); } }
🌐
Quora
quora.com › How-can-I-remove-leading-and-trailing-zeroes-from-a-float-in-Java
How to remove leading and trailing zeroes from a float in Java - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
🌐
GitHub
github.com › doocs › leetcode › blob › main › solution › 2700-2799 › 2710.Remove Trailing Zeros From a String › README_EN.md
leetcode/solution/2700-2799/2710.Remove Trailing Zeros From a String/README_EN.md at main · doocs/leetcode
Input: num = "123" Output: "123" Explanation: Integer "123" has no trailing zeros, we return integer "123". ... We can traverse the string from the end to the beginning, stopping when we encounter the first character that is not 0.
Author: doocs