split the string

joshishree
Kilo Contributor

Bhagyashree Pradeep Joshi,ggg

var array = name.split(",");

var newname =array[0];

alert(newname);

var name2 = newname.split(" ");

var firstname1 = name2[0];

var lastname2 = name2[1];

alert(firstname1);

alert(lastname2);

here ,alert(firstname1); = Bhagyashree

alert(lastname2); =   Pradeep

I want 'bhagyashree' in one variable and 'Pradeep Joshi' in another variable

how to achieve that

8 REPLIES 8

Dave Smith1
ServiceNow Employee
ServiceNow Employee

You simply need to extract the third element of your newname array:



var nextname = name2[2];



Then concatenate those variables:


var another_variable = lastname2 + " " + nextname;


nb: as you're new to scripting, "name", "newname", "firstname1" aren't great variable names.  



Also, it's an idea to alert() some text rather than just the variable - if the variable is blank, you'll get some confusing popup. Better to use:




alert("The value of firstname1 is: " + firstname1);


yup its working


Sharique Azim
Mega Sage

Hi ,



Whats wrong with the script?? it looks fine, and it works in my demo.


sharique azim wrote:


Whats wrong with the script??


It doesn't meet his requirements:


Bhagyashree Joshi wrote:



I want 'bhagyashree' in one variable and 'Pradeep Joshi' in another variable