11 Vote

JavaScript: Explode String Function

Question by Compi | 2017-12-15 at 18:17

From other programming languages, we all know the explode-function with which you can easily divide a string at a delimiter character or a separator string to multiple parts in order to get an array.

I have now tried several possibilities to use such a function also in JavaScript, but JavaScript seems not to know an explode. What can I do?

1Best Answer1 Vote

JavaScript indeed has an explode function. However, there, it is not called explode but .split().

Here is a small example how to use of this function.

var str = "one two three";
var arr = str.split(" ");

Here, we want to separate the string "one two three" by a space. So, we have to pass a space to the split-function. After that, the variable arr is an array with the three elements "one", "two" and "three".

By the way, if you pass an empty string, for example s.split (""), you will get back an array that contains every single letter of the original string as an individual element.
Last update on 2020-10-01 | Created on 2017-12-16

ReplyPositive Negative

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.


Ask your own question or write your own article on askingbox.com. That’s how it’s done.