Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript string separated by a comma

I'm trying to get everything before/after a comma from a string

var test = 'hello,world';

Result:

   var one = 'hello';
   var two = 'world';

What would be a good way to this?

Thanks


2 Answers

.split

Extra text because I need to write 15 characters for this submission to be approved.

-- edit

okay, more explicitly:

var k = "a,b".split(",");
alert(k[0]);
alert(k[1]);
like image 50
Noon Silk Avatar answered Sep 15 '26 16:09

Noon Silk


var test = 'hello,world',
    words = test.split(',');


   var one = words[0]; // hello
   var two = words[1]; // world
like image 24
Christian C. Salvadó Avatar answered Sep 15 '26 17:09

Christian C. Salvadó



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!