priti
priti

Reputation:

substring function to separate two strings using javascript

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

Answers (3)

Jan Hančič
Jan Hančič

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

Xn0vv3r
Xn0vv3r

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

unwind
unwind

Reputation: 400129

Sounds like you want split(). You would use it like this:

a = "X,Y"
b = a.split(",")

This would create an array of the strings "X" and "Y" and put that in b.

Upvotes: 5

Related Questions