String into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.
string.split(separator, limit) split() split(separator)
Note: This method does not change the original string and If (” “) is used as a separator, the string is split between words.
A simple example code divides a string into a list of substrings and returns them as an array.
<!DOCTYPE html> <html> <body> <script > const str = 'Hello World!'; const words = str.split(' '); console.log(words); const chars = str.split(''); console.log(chars); const strCopy = str.split(); console.log(strCopy); </script> </body> </html>
Use the limit parameter:
const myArray = text.split(" ", 3);// How,are,you
Split a string into characters and return the second character:
let text = "How are you doing today?"; const chars = text.split(""); console.log(chars); // 0
Do comment if you have any doubts or suggestions on this Js string split topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.