-4

If I get two values separated by "-" like:

"first value-second value"

What's a good way to find both in JS?

Thanks!

Hommer Smith
  • 24,944
  • 53
  • 156
  • 273

2 Answers2

4
var str = "first value-second value",
    arr = str.split('-');
console.log(arr[0]); //'first value'
console.log(arr[1]); //'second value' 
spliter
  • 11,751
  • 4
  • 29
  • 36
3

The split() method is used to split a string into an array of substrings, and returns the new array.

var str = "first value-second value",
var value = str.split("-");
alert(value[0]);// print first value
alert(value[1]);// print second value
Alessandro Minoccheri
  • 34,369
  • 22
  • 118
  • 164