I want to fetch last word dynamically

chandan86patra
Tera Contributor

Hi All ,

 

I want to fetch the last word from the short description and store in the one of the variable :

var gr= current.short_description;


var words = s.trim().split(gr);
var lengthOfLastWord = words[words.length - 1];
gs.print(lengthOfLastWord); 

 

var ab=lengthOfLastWord;

 

This script is not fetching the last word .Can anyone help me .

 

Thanks and Regards,

Chandan

2 REPLIES 2

SN_Learn
Kilo Patron
Kilo Patron

Hi @chandan86patra ,

 

Please try the below:

var sentence = "The world works with ServiceNow";
var words = sentence.split(" ");
var lastWord = words[words.length - 1];

gs.log("The last word of sentence is: " + lastWord);

 

Output:

*** Script: The last word of sentence is: ServiceNow

 

You can replace the string to your short description attribute as below:

var sentence = current.short_description;
var words = sentence.split(" ");
var lastWord = words[words.length - 1];

gs.log("The last word of sentence is: " + lastWord);

 

Please hit like and mark my response as correct if that helps.

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

Sandeep Rajput
Tera Patron
Tera Patron

@chandan86patra Please try the following script. It should work.

 

var gr= current.short_description;
var words = gr.trim().split(' ');
var lengthOfLastWord = words[words.length - 1];
gs.print(lengthOfLastWord); 
var ab=lengthOfLastWord;

Hope this helps.