Split String by Character

To split a string by given character in JavaScript, call split() method on the string and pass given character (delimiter string) as argument in the method call.

The expression to split a string str by a character ch is

str.split(ch);

where the datatype of ch is also string, but we specify only one character in the string.

Example

In the following example, we take a string in str, split the string by hyphen character '-', and display the splits array in pre#output.

index.html

ADVERTISEMENT

Conclusion

In this JavaScript Tutorial, we learned how to split the given string by a specific character using String.split() method, with example program.