How to get the data before separator.

ServiceNow de16
Tera Contributor

Hello,

How to get first word from the below data which is using the separator.

ex: Name|email from this I need Name which before the separator |.

Please help me on this one 

Thanks!

1 ACCEPTED SOLUTION

Pavankumar_1
Mega Patron

Hi,

If you need first string from this data Please try below script you will get the first string.

var str="Name|Email";
var n=str.indexOf("|");
var firstword=str.substring(0,n);
gs.info(firstword);

find_real_file.png

find_real_file.png

 

Hope it helps!!

Please Mark āœ… Correct, Thanks!! 

 

Regards

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

View solution in original post

3 REPLIES 3

Pavankumar_1
Mega Patron

Hi,

If you need first string from this data Please try below script you will get the first string.

var str="Name|Email";
var n=str.indexOf("|");
var firstword=str.substring(0,n);
gs.info(firstword);

find_real_file.png

find_real_file.png

 

Hope it helps!!

Please Mark āœ… Correct, Thanks!! 

 

Regards

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

-O-
Kilo Patron
Kilo Patron
var str = "Name|Email";

gs.info(str.split('|').shift());

This one also works if for some reason, sometimes str will not contain the separator. E.g

var str = "Name Email";

gs.info(str.split('|').shift());

will print Name Email as apposed to an empty string. Though maybe that is not what is required?

asifnoor
Kilo Patron

Here is the simple way

var str="Name|Email";
var name = str.split("|")[0].toString();
gs.print(name);

Mark the comment as a correct answer and also helpful if this helps to solve the problem.