
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Remove Leading Zeroes from a String in Java using regular expressions
The replaceAll() method of the String class accepts two strings representing a regular expression and a replacement String and replaces the matched values with given String.
Following is the regular expression to match the leading zeros of a string −
The ^0+(?!$)";
To remove the leading zeros from a string pass this as first parameter and “” as second parameter.
Example
The following Java program reads an integer value from the user into a String and removes the leading zeroes from it using the Regular expressions.
import java.util.Scanner; public class LeadingZeroesRE { public static String removeLeadingZeroes(String str) { String strPattern = "^0+(?!$)"; str = str.replaceAll(strPattern, ""); return str; } public static void main(String args[]){ Scanner sc = new Scanner(System.in); System.out.println("Enter an integer: "); String num = sc.next(); String result = LeadingZeroesRE.removeLeadingZeroes(num); System.out.println(result); } }
Output
Enter an integer: 000012336000 12336000
- Related Articles
- Explain how to remove Leading Zeroes from a String in Java
- How to remove consonants from a string using regular expressions in Java?
- How to remove vowels from a string using regular expressions in Java?
- Remove Leading Zeros From String in Java
- JavaScript Regex to remove leading zeroes from numbers?
- Remove Leading Zeros from a String in C#
- Java Program to remove leading and trailing whitespace from a string
- How to extract numbers from a string using regular expressions?
- Remove leading zeros from a Number given as a string using Python
- Java Program to remove the leading and trailing quotes from a string
- Return a specific MySQL string using regular expressions
- Trim a string in Java to remove leading and trailing spaces
- Removing leading zeros from a String using apache commons library in Java
- How to split a string using regular expressions in C#?
- Date validation using Java Regular Expressions

Advertisements