Here is an approach using str.slice(0, -n).
Where n is the number of characters you want to truncate.
var str = 1437203995000;
str = str.toString();
console.log("Original data: ",str);
str = str.slice(0, -3);
str = parseInt(str);
console.log("After truncate: ",str);
Answer from Hari Das on Stack Overflow Top answer 1 of 4
207
Here is an approach using str.slice(0, -n).
Where n is the number of characters you want to truncate.
var str = 1437203995000;
str = str.toString();
console.log("Original data: ",str);
str = str.slice(0, -3);
str = parseInt(str);
console.log("After truncate: ",str);
2 of 4
70
Remove last 3 characters of a string
var str = '1437203995000';
str = str.substring(0, str.length-3);
// '1437203995'
Remove last 3 digits of a number
var a = 1437203995000;
a = (a-(a%1000))/1000;
// a = 1437203995
Removing last 3 characters from a string? (SQLite)
Is there a way to just cut the last three digits off? SELECT SUBSTR(yourcolumn, 1, LENGTH(yourcolumn)-3 ) FROM ... More on reddit.com
Replace last character in string
Just depends how complex you want to make it. You could technically do it all in the pipeline: 'East1','West2' | ForEach-Object { if ($_[-1] -eq '1') { ($_.substring(0,$_.length-1) + '2') } else { ($_.substring(0,$_.length-1) + '1') } } East2 West1 More on reddit.com
What is the easiest way to remove last 3 characters in JavaScript?
A: The easiest way can be using the slice() method, which is straightforward and efficient for this use case.
sqlpey.com
sqlpey.com › javascript › remove-last-3-characters
Top 4 Ways to Remove Last 3 Characters from a String or Number ...
Can I apply these methods to other data types?
A: Yes, while the examples focus on numbers and strings, similar string manipulation techniques can be adapted for any data type that can be represented as a string.
sqlpey.com
sqlpey.com › javascript › remove-last-3-characters
Top 4 Ways to Remove Last 3 Characters from a String or Number ...
javascript remove last 3 characters from string
03:30
How to Remove the Last Character from a String in JavaScript - YouTube
How do I chop/slice/trim off last character in string using ...
05:20
Remove First and Last Character from a string using 3 ways in ...
03:16
Remove First and Last Character from String in JavaScript - YouTube
Scaler
scaler.com › home › topics › remove last character from string in javascript
Remove the Last Character from a String in JavaScript - Scaler Topics
May 4, 2023 - Use the replace() Method The replace() function in javascript can also be used the remove the last character from the string. The replace() function takes second parameter as the whole string and the first parameter is for matching string and instructing the number of characters needed to be ...
sqlpey
sqlpey.com › javascript › remove-last-3-characters
Top 4 Ways to Remove Last 3 Characters from a String or Number in JavaScript
November 23, 2024 - You might want to truncate it to remove the last three digits, making it easier to read or manipulate. Below are several approaches you can use to achieve this. One simple approach is to utilize the slice() method. This method allows you to specify the starting and ending indices to extract ...
YouTube
youtube.com › watch
How to Remove the Last 2,3,4, N Characters from a String in JavaScript - YouTube
To remove the last 2, 3, 4, and N characters from a string in JavaScript using different methods by w3school. Here are some examples to remove the last 2,3,4...
Published: December 16, 2023
Mastering JS
masteringjs.io › tutorials › fundamentals › remove-last-character
How to Remove the Last Character from a String in JavaScript - Mastering JS
November 12, 2021 - Using /.$/ as the regular expression argument matches the last character of the string, so .replace(/.$/, '') replaces the last character of the string with an empty string. let str = 'Masteringjs.ioF'; str.substring(0, str.length - 1); // Masteringjs.io str.substr(0, str.length - 1); // Masteringjs.io str.replace(/.$/, ''); // Masteringjs.io · With replace(), you can specify if the last character should be removed depending on what it is with a regular expression.
Medium
medium.com › @onlinemsr › how-to-remove-the-last-character-from-a-string-in-javascript-3d1c238d1669
How to Remove the Last Character from a String in JavaScript | by Raja MSR | Medium
February 15, 2024 - Removing the last character from a string can be tricky, as there are different methods and scenarios to consider. ... By the end of this blog post, you will have a better understanding of how to use JavaScript to remove the last character from a string, and how to apply this skill to various situations.
Medium
medium.com › @ryan_forrester_ › javascript-remove-last-character-from-string-fc1cabdf2722
Javascript Remove Last Character From String | by ryan | Medium
September 10, 2024 - For strings with only one character, removing the last character will result in an empty string. This behavior should be acceptable for most applications, but it’s important to be aware of it. ... Strings in JavaScript are immutable, meaning the original string is not modified by operations like slice() or substring().
GeeksforGeeks
geeksforgeeks.org › javascript-program-to-remove-last-character-from-the-string
JavaScript Program to Remove Last Character from the String | GeeksforGeeks
June 17, 2024 - In this approach, we will use substring() method for removing last character of string. The string.substring() is an inbuilt function in JavaScript that is used to return the part of the given string from the start index to the end index.
Top answer 1 of 5
1
No worries @StewartF, · · Try this... I think we're there now. · To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos. · Thanks, Robbie · var encQBySN = "sys_id=79ec46839742c2d032ea7a200153af1a"; //Adjust accordingly for testing. Comment out when happy with the results · var computersCount = 0; //Used for sanity check when testing · var computersNameCPU = 0; · var computersGR = new GlideRecord('cmdb_ci_vmware_instance'); //Change this table as required for specific CI class or type · //computersGR.addEncodedQuery(encQ); · computersGR.addEncodedQuery(encQBySN); · computersGR.setLimit(10); //Adjust this number or comment it out completely to run against all records · computersGR.query(); · while(computersGR.next()){ · computersCount++; · var computerName = computersGR.getValue('name'); · var computerSysID = computersGR.getValue('sys_id'); · var endsWith = computerName.endsWith("CPU"); //Note - endWith method is case sensitive so 'cpu' will not match · if(endsWith){ //Only interested in computers ending with CPU · computersNameCPU++; · gs.print('computerName: ' + computerName + ' Sys ID:' + computerSysID); //Comment when running as a bulk action against all Computers · var newComputerName = computerName.substring(0,computerName.length-3); · gs.print('TRIMMED computerName: ' + newComputerName + ' Sys ID:' + computerSysID); · computersGR.setValue('name', newComputerName); · //computersGR.update() //Comment and uncomment as appropriate · } · } · gs.print('Total computers checked: ' + computersCount) //Verify expected reults · gs.print('Total computers ending in CPU: ' + computersCount) //Verify expected reults · View solution in original post
2 of 5
0
Hi @StewartF, · · This is easily achieved using the following script. · Best practice tip - Create this as a 'Fix Script' so it can be reused (if required) but most importantly can be rolled back if required. - See below Screenshot and syntax. · Note - If you need to roll back any of the changes, navigate to the rollbacks by typing 'sys_rollback_context.LIST' into the nav menu. · PLEASE MAKE SURE YOU TEST AND ADAPT THE QUERY IN A LOWER ENVIRONMENT (NON PROD) BEFORE EXECUTING · To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos. · Thanks, Robbie · //var encQ = "operational_statusIN1,2,3,4,5" //An encoded query if you want to filter for certain records · var encQBySN = "serial_numberINVVM-370-F60616-KN,DVS-360-J56754-QH"; //Adjust accordingly for testing. Comment out when happy with the results · var count = 0; //Used for sanity check when testing · var computersGR = new GlideRecord('cmdb_ci_computer'); //Change this table as required for specific CI class or type · //computersGR.addEncodedQuery(encQ); · computersGR.addEncodedQuery(encQBySN); · //computersGR.setLimit(10); · computersGR.query(); · while(computersGR.next()){ · count++; · var computerName = computersGR.getValue('name'); · var computerSN = computersGR.getValue('serial_number'); · if(computerName.length > 20){ · gs.print('computerName: ' + computerName + ' Serial number:' + computerSN); //Comment when running as a bulk action against all Computers · var newComputerName = computerName.substring(0,computerName.length-3); · computersGR.setValue('name', newComputerName); · computersGR.update() //Comment and uncomment as appropriate · } · } · gs.print('Total computers: ' + count) //Verify expected reults