Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to split values in the array when value contains '-'?

User205031
Tera Contributor

Hi All,

 

I am trying to get values of left side of '-' in String. Could someone help me to figure out.

 

Suppose my string is : ABC - DEF

 

I want to print 'ABC'.

 

Please help!

1 ACCEPTED SOLUTION

Shruti
Giga Sage
Giga Sage
var result = [];
var input = "ABC-DEF,PQR-XYZ,STU-EFG";

var str = input.split(',');

for(var  i=0 ; i<str.length;i++){
	var new_str = str[i].split('-');
	result.push(new_str[0]);
}
gs.info(result);

View solution in original post

3 REPLIES 3

Shruti
Giga Sage
Giga Sage
var result = [];
var input = "ABC-DEF,PQR-XYZ,STU-EFG";

var str = input.split(',');

for(var  i=0 ; i<str.length;i++){
	var new_str = str[i].split('-');
	result.push(new_str[0]);
}
gs.info(result);

Thanks! It worked.

Runjay Patel
Giga Sage

Hi @User205031 

 

var strVal= "ABC-DEF";

var arr= strVal.split(',');

var leftVal = arr[0];
var rightVal = arr[1];

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------