EN
JavaScript - split string by hyphen sign (minus sign - ascii 45 code)
1
points
In this article, we would like to show you how to split string by hyphen sign in JavaScript.
String split()
function takes as parameter character by which the string will be split into an array of substrings. As the result of String split()
we get an array. To access an element we just get an element by its index.
Practical example
// ONLINE-RUNNER:browser;
var text = 'split-this-text';
var split = text.split('-');
var element1 = split[0];
var element2 = split[1];
var element3 = split[2];
console.log(element1); // split
console.log(element2); // this
console.log(element3); // text
Output:
split
this
text