-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
  • 26,772
  • 56
  • 167
  • 296

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
  • 12,321
  • 4
  • 33
  • 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
  • 35,521
  • 22
  • 122
  • 171