I have a string like below and I want to split it and get the value before pipe (|)
.
var str = "110|paris";
I just want to store like below,after split
var value = 100;
I have a string like below and I want to split it and get the value before pipe (|)
.
var str = "110|paris";
I just want to store like below,after split
var value = 100;
This:
var value = str.split('|')[0];
will give you what's before the pipe. More generally:
var array = str.split('|');
this will give you an array with elements ['110', 'paris']
so:
array[0] // this is '110'
array[1] // this is 'paris'
Note that if you actually want a number 110
and not a string '110'
then you should use parseInt
:
parseInt(array[0], 10) // this is 110
Full example:
var str = '110|paris';
var array = str.split('|');
var value = parseInt(array[0], 10);
var city = array[1];
Now value
is 100
(number) and city
is 'paris'
(string).