- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2025 03:11 AM
Hi Everyone,
I have a string, when I convert this string into array. The last element of the array is empty; I want to break the for loop when element is empty. Please help me with the issue.
sting = John, \n Guest 1, \n Guest 2, \n
var result = ' ';
var array = string.split(' , ');
for( var i = 0 ; i < array.length ; i++){
result += array[i] + 'joined the session';
}
result += "";
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2025 03:39 AM
try this and it worked for me
var string = "John, \n Guest 1, \n Guest 2, \n";
var result = '';
var array = string.split(', \n');
for (var i = 0; i < array.length; i++) {
if (array[i].trim() === '') {
break; // Break the loop if the element is empty
}
result += array[i].trim() + ' joined the session\n';
}
gs.info(result);
Output:
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2025 03:30 AM
Hello @Abhijit Das7
use below script:- If empty means = '' or whatever you can replace accordingly.
sting = John, \n Guest 1, \n Guest 2, \n
var result = ' ';
var array = string.split(' , ');
for( var i = 0 ; i < array.length ; i++){
//ADD below two lines - if this is what you meant by empty
if(array[i]=='')
break;
result += array[i] + 'joined the session';
}
result += "";
Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket.
Regards,
Shivalika
My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194
My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2025 03:39 AM
try this and it worked for me
var string = "John, \n Guest 1, \n Guest 2, \n";
var result = '';
var array = string.split(', \n');
for (var i = 0; i < array.length; i++) {
if (array[i].trim() === '') {
break; // Break the loop if the element is empty
}
result += array[i].trim() + ' joined the session\n';
}
gs.info(result);
Output:
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2025 03:41 AM - edited 04-15-2025 03:42 AM
Hello @Abhijit Das7 ,
Added a pointer to count the length of array and stored with less one value.
var sting = "John, \n Guest 1, \n Guest 2, \n";
var result="";
var array = sting.split(',');
var leg = --array.length; //storing the array length -1
for( var i = 0 ; i < leg ; i++){
result += array[i] + 'joined the session';
}
gs.print(result);
Output:
Is this the desired output.
If this solution helped resolve your issue, please consider marking it as helpful or correct.
This will assist others in finding the solution faster and close the thread.