我有像X,Y这样的字符串。我想用javascript将X与Y分开。请描述如何成为我在javascript的新手
答案 0 :(得分:5)
答案 1 :(得分:1)
您可以在字符串上使用split()方法,将字符串拆分为数组:
var myString = "X,Y";
var myArray = myString.split ( "," );
然后,myArray将在索引0上包含“X”,在索引1上包含Y
或者您可以使用substring方法:
var myString = "X,Y";
var myX = myString.substring ( 0, myString.indexOf ( "," ) );
var myY = myString.substring ( myString.indexOf ( "," ) + 1 );
答案 2 :(得分:0)
尝试:
var Test= "X,Y";
var Part = Test.slice(0, 1);
或
var Test = "X,Y";
var Part = Test.substr(0, 1);