Last Updated : Jan 12, 2023
A new string will be returned that contains the specified changes while the original string remains unchanged.
The substr() function takes two parameters to alter the given string.
The first parameter is the index from which the new string will begin and the second parameter is the length of the new string starting from the given index.
A string or text can be replaced with another string or empty string by using the replace function.
The replacement function takes two parameters: a string and a text to be replaced.
The string parameter is the first parameter, while the text parameter is the second parameter. To remove the text from the string the second parameter should be given as an empty string.
Matching the substring and removing that part of the string is accomplished by using the replace () method.
It is also possible to match the substring perfectly using regular expressions in the replace () method.
Substrings provided in the first argument are replaced with substrings provided in the second argument.
<html> <head> </head> <body> <p id="a"></p> <p id="a1"></p> <script> let a = 'Hello world' document.write(a.substr(0, 5),"<br>") document.write(a.substr(6),"<br>") document.write(a.substr(4, 3)) </script> </body> </html>
- In our first step, we write <HTML>, which tells the browser what HTML version we're using. Documents in HTML begin with tags.
- Using the <head> tag, we will describe the project's heading.
- This is followed by the <body> tag that defines the body of the webpage. Throughout the website, all the content is written here.
- Then we create a paragraph using p id.
- Then we can be added to an HTML page with a <script> tag. For creating substring.
- Then we remove the string using again the creation of a substring.
- After that </script></body></html> and code should be executed after closing all tags.