How to Split String By Particular Character
Here are the steps to split string by particular character. Generally people split a sentence into words by splitting it by whitespace characters. But sometimes you may need to split your text by a custom character. Let us say you have the following string.
var input = 'john smith$123 Street$Apt 42$New York$NY$12345';
Let us say you want to split the above text by character ‘$’ instead of space. You can do easily do this using split() function available for each string, out of the box. Here is the command to split the above text by $ sign.
var words = input.split('$');
The above command will split the string into an array of substrings and store it in words variable.
['john smith','123 Street','Apt 42','New York','NY','12345']
Once you have split the string into an array of substrings, you can access individual substrings using the array name and index.
var name = words; //john smith var street = words; //123 Street ...
If you don’t specify the character for splitting in split() function, it will split your string by space character, by default.