In this article we will show you the solution of JavaScript substring remove last character, we will use three different methods to remove the last character using JavaScript.
The methods are - Using the substring method, Using the slice method and Using the array and pop method
Step By Step Guide On JavaScript Substring Remove Last Character :-
Method 1 - Using the substring method
In this method, we will use substring to remove the last character using JavaScript.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> JavaScript substring remove the last character</title> <style> h1 { font-size: larger; font-weight: bolder; color: lightgreen; } </style> </head> <body> <h1>TALKERSCODE</h1> <h3> JavaScript substring remove the last character </h3> <script> function removeChar( str ) { //substring method return str.substring ( 0 , str.length -1 ) } ; </script> </body> </html>
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- As above now the <head> tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
- <h1> tag used to add heading here.
- we have created the<script> tag to add JavaScript.
- First create function named by removeChar ( str )
- Setting str.length to -1 to remove the last character
Method 2 - Using the slice method
In this method, we will use slice to remove the last character using JavaScript.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> JavaScript substring remove the last character</title> <style> h1 { font-size: larger; font-weight: bolder; color: lightgreen; } </style> </head> <body> <h1>TALKERSCODE</h1> <h3> JavaScript substring remove the last character </h3> <script> function removeChar( str ) { //slice method return str.slice ( 0 , -1 ) } ; </script> </body> </html>
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- As above now the <head> tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
- <h1> tag used to add heading here.
- we have created the<script> tag to add JavaScript.
- First create function named by removeChar ( str )
- Setting the str.slice ( 0 , -1 ) to remove the last character
Method 3 - using array and pop method
In this method we will convert the string into array and then use the pop method to remove the last character.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> JavaScript substring remove the last character</title> <style> h1 { font-size: larger; font-weight: bolder; color: lightgreen; } </style> </head> <body> <h1>TALKERSCODE</h1> <h3> JavaScript substring remove the last character </h3> <script> function removeChar( str ) { //array and pop method let arr = str.split ( ' ' ) arr.pop () return arr.join ( ' ' ) } ; </script> </body> </html>
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- As above now the <head> tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
- <h1> tag used to add heading here.
- we have created the<script> tag to add JavaScript.
- First create a function named by removeChar ( str )
- Then convert the string to an array
- Then split the array
- Using pop method to remove the last character.
- Then join the array again
Conclsion :-
At last here in conclusion, here we can say that with the help of this article we are able to remove the last character in a string using JavaScript.
I hope this article on JavaScript substring remove last character helps you and the steps and method mentioned above are easy to follow and implement.