Remove Leading Zeros From String in Java (original) (raw)

Last Updated : 10 Jan, 2025

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.

**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);
}

}

`

**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

**Example:

Java `

// Java program to Remove leading/preceding // zeros from a given String

// Importing required classes import java.util.Arrays; import java.util.List;

public class Geeks { public static String removeLeadingZeros(String str) { int n = str.length(); int f = 0;

    // Find the index of the first non-zero character
    while (f < n && str.charAt(f) == '0') {
        f++;
    }

    // Directly store the result
    String r
        = str.substring(f, n); // Highlighted change
    return r;
}

public static void main(String[] args)
{
    String s = "00000123569";
    System.out.println(
        removeLeadingZeros(s)); // Output: 12356090
}

}

`

**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.

Similar Reads

Java Basic Programs





















Java Pattern Programs














Java Conversion Programs















Java Classes and Object Programs
















Java Methods Programs











Java Searching Programs




Java 1-D Array Programs













Java 2-D Arrays (Matrix) Programs













Java String Programs