Reputation:
I have strings like X,Y. I want to separate X from Y using javascript. Please describe how to as I am new at javascript
Upvotes: 0
Views: 3149
Reputation: 53929
You could use the split() method on a string, which will split the string into a array:
var myString = "X,Y";
var myArray = myString.split ( "," );
myArray will then contain "X" on index 0, and Y on index 1
Or you could use the substring method as so:
var myString = "X,Y";
var myX = myString.substring ( 0, myString.indexOf ( "," ) );
var myY = myString.substring ( myString.indexOf ( "," ) + 1 );
Upvotes: 1
Reputation: 18184
Try:
var Test= "X,Y";
var Part = Test.slice(0, 1);
or
var Test = "X,Y";
var Part = Test.substr(0, 1);
Upvotes: 0