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.
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.
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);
   }
}
Enter an integer value as a String
00126718
126718
Following Java program reads an integer value from the user into a String and removes the leading zeroes from it using the StringBuffer class.
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);
   }
}
Enter an integer:
00012320002
12320002
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 = TailingZeroesRE.removeLeadingZeroes(num);
      System.out.println(result);
   }
}
Enter an integer:
000012336000
12336000
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.
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);
   }
}
Enter an integer:
000125004587
125004587