简体   繁体   中英

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

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.

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 );

Try:

var Test= "X,Y";
var Part = Test.slice(0, 1);

or

var Test = "X,Y";
var Part = Test.substr(0, 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM