- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2017 05:02 PM
Hi Everyone,
I have a question... if i would like to remove last of final characters, please check below example:
var test = 'Group';
var test1 = 'Name';
var test2 = ' ';
var test3 = ' ';
var test4 = ' ';
var str1 = "ACCESS"+"_"+test+"_"+test1+"_"+test2+"_"+test3+"_"+ test4;
gs.print(str1);
rep1 = str1.replace("_", " ");
gs.print(rep1);
I'll try to remove final last or final "_" from the group name but this removes all "_"
how can I only remove the last "_" from the name? also some times it will be 3 or 2 also 1 at the end of the group name
thanks for your help
RD
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2017 02:58 PM
Please check if this helps.
var finalStr = '';
var test = 'Group';
var test1 = 'Name';
var test2 = ' ';
var test3 = ' ';
var test4 = ' ';
var str1 = "ACCESS"+"_"+test+"_"+test1+"_"+test2+"_"+test3+"_"+ test4;
gs.print(str1);
var strarray = str1.split(' ');
for(var i = 0; strarray.length > i; i++)
if(strarray[i] != '_')
finalStr = finalStr + strarray[i];
gs.print(finalStr.slice(0, -1));
Reference: Javascript chop/slice/trim off last character in string - Stack Overflow
JavaScript String slice() Method
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2017 11:26 AM
Hi Diaz,
Take test3 and test4 in separate object and replace from 2nd object and then append it to first object.
var str1 = "ACCESS"+"_"+test+"_"+test1+"_"+test2+"_";
var str2=test3+"_"+ test4;
rep1 = str2.replace("_", " ");
Final str = str1+str2;
gs.print(str);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2017 02:58 PM
Please check if this helps.
var finalStr = '';
var test = 'Group';
var test1 = 'Name';
var test2 = ' ';
var test3 = ' ';
var test4 = ' ';
var str1 = "ACCESS"+"_"+test+"_"+test1+"_"+test2+"_"+test3+"_"+ test4;
gs.print(str1);
var strarray = str1.split(' ');
for(var i = 0; strarray.length > i; i++)
if(strarray[i] != '_')
finalStr = finalStr + strarray[i];
gs.print(finalStr.slice(0, -1));
Reference: Javascript chop/slice/trim off last character in string - Stack Overflow
JavaScript String slice() Method
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2017 04:11 PM
thank you Shishir for your help
it is workig!
I really apreciate your help
RD
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2020 03:12 PM
I was looking for how to do this, and found this thread through a Google search; however, I don't find either of the answers proposed satisfying. You should not need to run for loops or create new variables to do something as simple as take the final character out of a string. Javascript has native functions that can leverage this ability. Now, the two answers above me do utilize some native functions, but they add in steps that we can avoid.
One day in the future, I hope that ServiceNow will support a trimRight() or trimEnd() function, but until then - we can use the .substring method to return our string with one (or more) character(s) less than it currently has - from either the start or the end.
Let's take the string "Hello World!" and remove the exclamation point at the end:
var str = "Hello World!";
return str.substring(0, str.length-1);
The output of this script will be "Hello World"
What if we had the string "_Hello World!" and wanted to return just the "Hello World!" portion?
var str = "_Hello World!";
return str.substring(1, str.length);
The output of this script will be "Hello World!"
The original question posed here doesn't really need to remove a final character exactly, as the _ happens before the last variable was added. But these solutions will do better to remove the final character from a string as needed.
I hope this will assist someone that's looking to accomplish this in the future.