Open In App
Related Articles

Remove Leading Zeros From String in Java

Improve Article
Improve
Save Article
Save
Like Article
Like

Given a string of digits, remove leading zeros from it.

Illustrations: 

Input : 00000123569
Output: 123569

Input: 000012356090
Output: 12356090

Approach: We use the StringBuffer class as Strings are immutable.

  • Count leading zeros by iterating string using charAt(i) and checking for 0 at the “i” th indices.
  • Converting a string into StringBuffer object as strings are immutable
  • Use StringBuffer replace function to remove characters equal to the above count using replace() method.
  • Returning string after removing zeros

Example:

Java




// Java program to Remove leading/preceding
// zeros from a given String
 
// Importing required classes
import java.util.Arrays;
import java.util.List;
 
// Main class
// RemoveZero
class GFG {
 
    // Method 1
    // to Remove leading zeros in a string
    public static String removeZero(String str)
    {
 
        // Count leading zeros
 
        // Initially setting loop counter to 0
        int i = 0;
        while (i < str.length() && str.charAt(i) == '0')
            i++;
 
        // Converting string into StringBuffer object
        // as strings are immutable
        StringBuffer sb = new StringBuffer(str);
 
        // The StringBuffer replace function removes
        // i characters from given index (0 here)
        sb.replace(0, i, "");
 
        // Returning string after removing zeros
        return sb.toString();
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
 
        // Sample string input
        String str = "00000123569";
 
        // Calling method 1 to count leading zeroes
        // in above string
        str = removeZero(str);
 
        // Printing leading zeros inside string
        System.out.println(str);
    }
}


Output

123569

Time Complexity: O(n), where n is the length of the input string. This is because the program iterates through the input string once to count the leading zeroes and then again to remove them using the StringBuffer replace function. 
Auxiliary Space: O(n) , as the program creates a new StringBuffer object with the same length as the input string.

Approach : Using substring() method

  • Finding the index of first non zero character
  • And then use substring method from that index to length of given string
  • Assigning the substring to new string

Example:

Java




// Java program to Remove leading/preceding
// zeros from a given String
 
// Importing required classes
import java.util.Arrays;
import java.util.List;
 
class GFG {
 
    public static void main(String[] args)
    {
 
        // Sample string input
        String str = "00000123569";
        String newstr = "";
        int ind = 0;
        for (int i = 0; i < str.length(); i++) {
            char p = str.charAt(i);
            if (p != '0') {
                ind = i;
                break;
            }
        }
        newstr = str.substring(ind, str.length());
        // Printing leading zeros inside string
        System.out.println(newstr);
    }
}


Output

123569

Time Complexity: O(n), where n is the length of the input string ‘str’. The algorithm performs a single loop through the length of the string to find the first non-zero character.
Auxiliary Space Complexity: O(n), where n is the length of the input string ‘str’. The algorithm requires a string variable ‘newstr’ to store the modified string without leading zeros.

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. 

Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.


Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Last Updated : 03 Feb, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials