- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
In a string, the charAt() method returns the character that is located at the given index (position). First character's index is 0, second character's index is 1.
The substring() method takes a string and extracts all characters between two indices (positions), then it returns the substring. Characters are taken out of a string using the substring() technique. The initial string is left alone when using the substring() technique.
Following is the syntax for substring() −
Splitting a string into an array of substrings is done using the split() method. split() produces a new array as its output. The string's original content is unaltered by the split() technique. When the separator ("") is applied, the string is divided into words.
Following is the syntax for split() −
n the following example, we are running a script and passing a value; later, we have declared var=result to get the output using split().
<!DOCTYPE html> <html> <body> <script> var result = ; const str1 = 'one:two;three'; result = str1.split(":"); document.write(result); </script> </body> </html>
When the script gets executed, it will generate an output consisting of the value "one," which will be displayed on the webpage as the event gets triggered by running the script.
Let’s look into the following example, where we are using split() for getting the desired output.
<!DOCTYPE html> <html> <body> <script> var s = 'HelloIm cat:Your arewelcome to tutorialspoint dog;'; var arrayStr = s.split(/[:;]/); alert(arrayStr); </script> </body> </html>
When the script gets run, the event gets triggered and displays the text of the index we have used in the script in the alertbox.
Consider the following example, here we are using the split() method, which divides the string into substrings, and the substrings are placed in an array. Later, we will pass an index value to get the desired output.
<!DOCTYPE html> <html> <body> <script> let str = 'xyz:welcome to tutorialspoint:66df88v:'.split(':') document.write(str) </script> </body> </html>
On running the above script, the output window will pop up, displaying a text on the webpage that was caused by the event that got triggered on running the script.
In the following example, we are running the script along with slice() and mentioning the indices to get the desired output on the screen page.
<!DOCTYPE html> <html> <body> <script> let str = 'tp:The Best E-Way Learning:007jb700:' document.write(str.slice(3, 35)) </script> </body> </html>
When the script gets executed, it will generate an output consisting of a text that was lying between (3, 35), (start value), and (end value) that is printed on the webpage obtained by an event that gets triggered on running the script.
Kickstart Your Career
Get certified by completing the courseGet Started