Trim/Remove first or last character in Javascript
While using C#, if you want to remove the first character or the last character then you can simply use one of the following lines of code:
//Trim startstring trimmed = str.TrimStart('?', '.', ',');
//Trim Endstring trimmed1 = str.TrimEnd('?', '.', ',');
//Trim start single characterstring trimsingle = str.TrimStart('?');Javascript’s trim method doesn’t accept any paramters. To do the same in Javascript, use the following methods:
Remove the first character
//Trim startvar myString = '!thisIsMyString';var sillyString = myString.substr(1);Remove the last character
//Trim endvar myString = 'thisIsMyString!';var sillyString = myString.slice(0, -1);Remove the first character and last character
//Trim start and endvar myString = '!thisIsMyString!';var sillyString = myString.substr(1).slice(0, -1);You can do the same in typescript, as typescript is the same as javascript with some additional features.
Learnt and shamelessly copied some code from Removing A Character From The Start/End of a String In Javascript