This write-up will demonstrate the methods to eliminate leading zeros from a string.
How to Remove Leading Zeros from a String in JavaScript?
For removing leading zeros from a string, utilize the methods listed below:
Let’s examine the working of each method separately.
Method 1: Remove Leading Zeros from a String Using parseInt() Method
The “parseInt()” method is used for removing the leading zeros from a String containing integer values. It accepts a string as an argument and gives it as output after removing the leading zeros.
Syntax
Follow the given syntax to use the parseInt() method:
Have a look at the following example to know more about the given method.
Example
First, we will create a string that contains numbers starting with zeros:
Then, we will call the parseInt() method to remove the leading zeros by passing the string to the method and store it in a variable named “numb”:
Finally, print the returned value on the console:
The given output indicates that the leading zeros are successfully removed from the string:
If you want to remove the leading zeros from a floating point number in a string, see the next given section.
Method 2: Remove Leading Zeros from a String Using parseFloat() Method
For removing leading zeros from a string that contains a floating point number, use the “parseFloat()” method. It also accepts a string as an argument.
Syntax
Follow the below syntax to use the parseFloat() method:
Example
Here, we will create a string containing the decimal number leading to zeros:
Then, call the parseFloat() method by passing the string in it as an argument and stores the resultant value in variable “numb”:
Finally, we will print the resultant value on the console using “console.log()” method:
As you can see that the given output shows that the zeroes leading from the string are successfully removed:
Let’s head towards the next method!
Method 3: Remove Leading Zeros from a String Using replace() Method
You can use the “replace()” method to eliminate leading zeros from a string. This method uses the pattern or regex that helps to replace something based on that regex.
Syntax
The following described method is used for the replace() method:
Here, “regex” is the pattern that helps to replace something with “replacer”.
Example
In this example, first we will create a string named “strng” and assign it a value having leading zeros:
Then, we will call the replace() method by passing two parameters; the regex, which is used to remove leading zeros from a string and the empty string that acts as a replacer:
Finally, we will print the resultant value using the “console.log()” method:
Output
Let’s move ahead to understand some other methods for removing leading zeros from the string.
Method 4: Remove Leading Zeros from a String Using Number Constructor
The Number() constructor takes a string as an argument and returns the number by truncating zero from the start of the string.
Syntax
Use the following syntax to utilize the Number constructor:
Example
We will create a new string named “strng” and assign value “000100”:
Then, pass the string to the Number constructor which will remove the starting zeros from the string:
Print the resultant value that is stored in the variable “numb”:
As you can see in the output, the starting zeros are removed from the string:
Let’s see another approach for removing zeros from a string.
Method 5: Remove Leading Zeros from a String by Multiplying it with 1
You can remove zeros placed before the number by multiplying the string with “1”. When the string multiplies with one, it removes the starting zeros and returns the actual number.
Syntax
Follow the given syntax for deleting the leading zeros from a string:
Example
We will create a string “strng” with value “001020”:
Then, multiply the string with one and store it in a variable “numb”:
Print the resultant value using “console.log()” method:
Output
We have gathered different methods for removing leading zeros from the string.
Conclusion
For removing leading zeros from a string, you can use parseInt() method, parseFloat() method, replace() method, Number() constructor, or multiply the string by 1. The parseInt() method returns the int value by removing leading zeros, while the parseFloat() method gives the floating-point number by truncating the leading zeros. The replace() method uses regex to remove zeros. In this write-up, we have demonstrated various methods for removing leading zeros from a string.