In JavaScript, to remove a string’s first character if it is “0”, the commonly used approaches are the substring() or slice() method to extract all characters of the string except for the first one. For checking the first character of the string, use the bracket notation [] or charAt() method by passing the first index that is “0”.
This article will describe the methods to delete the string’s first character if it is 0 in JavaScript.
How to Delete the First Character of the String if it is 0 in JavaScript?
To delete the first character of the string if it is “0”, use the following methods:
Method 1: Delete the First Character of the String if it is 0 Using “slice()” Method
In JavaScript, the “slice()” method is used to extract a part of an array/string and return a new array/string. The method takes two arguments; the start index and the end index. The end index is an optional argument.
Syntax
The syntax of the slice() method is as follows:
Or use the given syntax for the slice() method to delete the first character of the string it is 0:
</tr>
</tbody>
</table>
<strong>Example</strong>
Create a string that contains number starting with 0:
[cc lang="bash" width="100%" height="100%" escaped="true" theme="blackboard" nowrap="0"]
const string = '0010';
Now, use the conditional statement and check the 1st index of the string using the bracket notation. If the string’s first index contains “0”, then call the slice() method by passing the starting index of the string that is “1”. It will return a new string beginning at the first index of the original string. Else, print the same string on the console:
const str = string.slice(1);
console.log(str);
}
else {
console.log(string);
}
It can be seen that the first character of a string that is 0 has successfully been deleted from the string:
Now, verify it on another string where the first character is not 0:
Here, you can see that the else statement is executed:
Using slice() Method With “charAt()” Method
You can also use the “charAt()” method with the slice() method instead of the string’s index. This method is utilized to retrieve a particular character. It accepts one argument, the index of the character you want to retrieve.
Use the “charAt()” method in a conditional statement and then call the slice() method:
...
}
Output
Method 2: Delete the First Character of the String if it is 0 Using “substring()” Method
The “substring()” method is identical to the slice() method. It is used to extract/get the portion of a string and outputs a new string. It accepts two arguments; the starting index and the ending index. The ending index is an optional argument.
Syntax
The syntax of the substring() method is as follows:
Or use the below-mentioned syntax for the substring() method to delete the first character of the string it is 0:
Example
Create a string that stores a string “0010”:
Use the “charAt(0)” method in the conditional statement to check if the first character of the string is ‘0‘. If it is, then call the “substring(1)” method to extract all characters of the string except for the first one, and the resulting substring is assigned to the variable “str”:
const str = string.substring(1);
console.log(str);
}
else {
console.log(string);
}
Output
That’s all about deleting the first character of a string if it is 0 in JavaScript.
Conclusion
To delete the first character of the string if it is 0, use the “slice()” method or the “substring()” method with bracket notation [ ] or the charAt() method to check the first index of the string by passing the index “0” that is the first index of the string. This article described the methods to delete the string’s first character if it is 0 in JavaScript.