Languages
[Edit]
EN

JavaScript - how to split string by hyphen sign (minus sign - ascii 45 code)

1 points
Created by:
AnnLen
1885

In JavaScript string has split method. 

String split() function takes as parameter character by which the string will be splitted into array of substrings. As the result of String split() we get array. To access element we just get element by it's index.

Run below example to see how it works in practice.

// ONLINE-RUNNER:browser;

var text = 'split-this-text';
var split = text.split('-');

var element1 = split[0]; // split
var element2 = split[1]; // this
var element3 = split[2]; // text

console.log( element1 );
console.log( element2 );
console.log( element3 );

 

Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join