Reputation: 451
I have string in jQuery.
var s = "Text1, Text2, Text,true";
I need split this variable in two part:
var s1 = substring(...); //after this s1="Text1, Text2, Text"
var s2 = substring(...); //after this s2="true"
Can you help me split variable?
Upvotes: 8
Views: 13025
Reputation: 63065
var index = s.lastIndexOf(",");
var first = str.substr(0, index );
var last = str.substr(index + 1);
Or using slice
var first = str.slice(0, index);
var last = str.slice(index +1);
Upvotes: 3
Reputation: 113345
var s="Text1, Text2, Text,true";
var lastIndex = s.lastIndexOf(",")
var s1 = s.substring(0, lastIndex); //after this s1="Text1, Text2, Text"
var s2 = s.substring(lastIndex + 1); //after this s2="true"
Upvotes: 14
Reputation: 553
var s = "Text1, Text2, Text,true";
var n = s.lastIndexOf(",");
var s1 = s.substring(0, n);
var s2 = s.substring(n + 1);
Upvotes: 0
Reputation: 2230
s1 = s.split(',');
s2 = s1.pop();
s1 = s1.join(',');
(untestet)
Upvotes: 3
Reputation: 388316
Try
var array = s.match(/(.*),(.*)/);
var s1 = array[1];
var s2 = array[2]
Upvotes: 1