📋 Table Of Content
To split a string after a specific character we can use the
Split string at a specific Character
seperator - Any string or regular expression used for splitting the string.
Let's see an example to split the string at the dash (
let str = "Hello-how-are-you" const splitArr = str.split("-") console.log(splitArr)
[ 'Hello', 'how', 'are', 'you' ]
Here, we have applied the
split() method on the string and passed the special character, here dash (
-) as the argument to split the string whenever it encounters the character.
Once done, it returns us the array of substrings as the output.
split() method and return the first item from the array.
const str = "Batman Superman" console.log(str.split(" ")) // Batman
The split() method with the
" " as separator returns the array of substring and from there we extract the
0 index item i.e here is Batman.