Last Updated : Jan 1, 2023
Using this method, we can remove any characters because it is collects particular character index then it will removes that character from a string so not only first character it can remove any specific character in provided string.
Here we defined one string ‘Hello’ then using substring() method with value ‘1’ we easily removed first character of provide string ‘H’ will eliminated from that string and stored to variable ‘str’.
The substring() method extracts characters between two indices, from a string and returns the substring.
It will extracts characters from start to end. The substring() method does not change the original string.
<!DOCTYPE html> <html> <head> <title>Remove First Character From String</title> </head> <body> <script> let str='Hello'; str = str.substring(1); console.log(str); </script> </body> </html>
- <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
- The<html> tag is used to indicate the beginning of HTML document.
- As above shown <head> tag is contains information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
- Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
- <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
- In substring() method with value ‘1’ refers first character ‘H’ from string ‘Hello’ and it is extracts the ‘H’ from string then remaining sub string ‘ello’ stored to variable ‘str’.
- Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.
When we execute our program on browser we can’t see the result so we have use that shortcut ‘ctrl+shift+j’ then result will see printed on console by console.log().