[ACCEPTED]-Split variable using a special character in JavaScript-jquery

Accepted answer
Score: 52
values=i.split('*');
one=values[0];
two=values[1];

0

Score: 5

use string.split(separator, limit)

<script type="text/javascript">
var str="my*text";
str.split("*");
</script>

0

Score: 3

Just to add, the comma operator is your 1 friend here:

var i = "my*text".split("*"), j = i[0], k = i[1];
alert(j + ' ' + k);

http://jsfiddle.net/EKB5g/

Score: 1

You can use the split method:

var result = i.split('*');

The variable result 4 now contains an array with two items:

result[0] : 'my'
result[1] : 'text'

You 3 can also use string operations to locate 2 the special character and get the strings 1 before and after that:

var index = i.indexOf('*');
var one = i.substr(0, index);
var two = i.substr(index + 1, i.length - index - 1);

More Related questions