

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain how to remove Leading Zeroes from a String in Java
Whenever you read an integer value into a String, you can remove leading zeroes of it using StringBuffer class, using regular expressions or, by converting the given String to character array.
Converting to character array
Following Java program reads an integer value from the user into a String and removes the leading zeroes from it by converting the given String into Character array.
Example
import java.util.Scanner; public class LeadingZeroes { public static String removeLeadingZeroes(String num){ int i=0; char charArray[] = num.toCharArray(); for( ; i<= charArray.length; i++){ if(charArray[i] != '0'){ break; } } return (i == 0) ? num :num.substring(i); } public static void main(String args[]){ Scanner sc = new Scanner(System.in); System.out.println("Enter an integer: "); String num = sc.next(); String result = LeadingZeroes.removeLeadingZeroes(num); System.out.println(result); } }
Output
Enter an integer value as a String 00126718 126718
Using StringBuffer class
Following Java program reads an integer value from the user into a String and removes the leading zeroes from it using the StringBuffer class.
Example
import java.util.Scanner; public class LeadingZeroesSB { public static String removeLeadingZeroes(String num){ int i=0; StringBuffer buffer = new StringBuffer(num); while(i<num.length() && num.charAt(i)=='0') i++; buffer.replace(0, i, ""); return buffer.toString(); } public static void main(String args[]){ Scanner sc = new Scanner(System.in); System.out.println("Enter an integer: "); String num = sc.next(); String result = LeadingZeroesSB.removeLeadingZeroes(num); System.out.println(result); } }
Output
Enter an integer: 00012320002 12320002
Using regular expressions
Following Java program reads an integer value from the user into a String and removes the leading zeroes from it using the Regular expressions.
Example
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 = TailingZeroesRE.removeLeadingZeroes(num); System.out.println(result); } }
Output
Enter an integer: 000012336000 12336000
Using apache commons 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>
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.
Example
import java.util.Scanner; import org.apache.commons.lang3.StringUtils; public class LeadingZeroesCommons { public static String removeLeadingZeroes(String str){ str = StringUtils.stripStart(str, "0"); 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 = LeadingZeroesCommons.removeLeadingZeroes(num); System.out.println(result); } }
Output
Enter an integer: 000125004587 125004587
- Related Questions & Answers
- Remove Leading Zeroes from a String in Java using regular expressions
- JavaScript Regex to remove leading zeroes from numbers?
- Remove Leading Zeros From String in Java
- Java Program to remove leading and trailing whitespace from a string
- Remove Leading Zeros from a String in C#
- Java Program to remove the leading and trailing quotes from a string
- Display numbers with leading zeroes in Java
- Trim a string in Java to remove leading and trailing spaces
- How to remove leading zeros from a number in JavaScript?
- How to remove white spaces (leading and trailing) from string value in MongoDB?
- Java Program to Remove leading zeros
- How to remove all leading whitespace in string in Python?
- How to remove all trailing and leading whitespace in a string in Python?
- How to remove leading and trailing whitespace from a MySQL field value?
- Trim (Remove leading and trailing spaces) a string in C#