开发者

Split variable using a special character in JavaScript

I have a variable var i = "my*text" I want to split it using special character *. I mean, I want t开发者_C百科o generate var one = "my" and var two = "text" from the above variable.

How can I do this using jQuery and (or) JavaScript? .


values=i.split('*');
one=values[0];
two=values[1];


use string.split(separator, limit)

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


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

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

http://jsfiddle.net/EKB5g/


You can use the split method:

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

The variable result now contains an array with two items:

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

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜