Explain how to remove Leading Zeroes from a String in Java

Java 8Object Oriented ProgrammingProgramming Scripts

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
raja
Published on 05-Jul-2019 07:45:14
Advertisements