If (” “) is used as a separator, the string is split between words. If the separator is not found in the input string, the method returns an array containing the entire input string as its single element.
Example: Using string.split() Method
const inputString = 'firstname.lastname@example.org'; const separator = '.'; const resultArray = inputString.split(separator); console.log(resultArray);
In this example, we used the split() method to break the inputString into an array of substrings, using the separator variable (‘.’) as the delimiter. The resulting array contains two elements: ‘nshah@gmail‘ and ‘com’.
const input_string = 'Jane~456 Boulevard~Unit 18~Los Angeles~CA~54321'; const [your_name, address, apartment, city, state, zipCode] = input_string.split('~'); console.log(your_name); console.log(address); console.log(apartment); console.log(city); console.log(state); console.log(zipCode);
Jane 456 Boulevard Unit 18 Los Angeles CA 54321