
- 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
Removing leading zeros from a String using apache commons library in Java
The stripStart() method of the org.apache.commons.lang.StringUtils class accepts two strings and removes the set of characters represented by the second string from the string of the first string.
To remove leading zeros from a string using apache communal library −
Add the following dependency to your pom.xml file
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency>
Get the string.
Pass the obtained string as first parameter and a string holding 0 as second parameter to the stripStart() method of the StringUtils class.
Example
The 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.
import java.util.Scanner; import org.apache.commons.lang3.StringUtils; public class LeadingZeroesCommons { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a String: "); String str = sc.nextLine(); String result = StringUtils.stripStart(str, "0"); System.out.println(result); } }
Output
Enter a String: 000Hello how are you Hello how are you
- Related Articles
- Remove Leading Zeros From String in Java
- Remove Leading Zeros from a String in C#
- Remove leading zeros from a Number given as a string using Python
- Remove Leading Zeros from an Array using C++
- Remove Leading Zeroes from a String in Java using regular expressions
- Add leading Zeros to Python string
- Add leading Zeros to Python Program string
- Java Program to Remove leading zeros
- Removing punctuations from a string using JavaScript
- How to remove leading zeros from a number in JavaScript?
- Java Program to add leading zeros to a number
- Removing all spaces from a string using JavaScript
- Explain how to remove Leading Zeroes from a String in Java
- Remove leading zeros in a JavaScript array?
- Java Program to remove leading and trailing whitespace from a string

Advertisements