How to Chop/Slice/Trim Off Last Character in String Using JavaScript

How do I chop/slice/trim off last character in string using Javascript?

You can use the substring function:

let str = "12345.00";str = str.substring(0, str.length - 1);console.log(str);

How can I remove the last character of a sentence in JavaScript?

Since the str.slice function returns the sliced part between two index as a substring, in your case, you should use str.slice(0,-1), that is equals to str.slice(0, str.length - 1). And it means that is going to return every character from zero index without the last character of the string passed.

function morseCode(str) {
morseCode = {
"A": ".- ",
"B": "-... ",
"C": "-.-. ",
"D": "-.. ",
"E": ". ",
"F": "..-. ",
"G": "--. ",
"H": ".... ",
"I": ".. ",
"J": ".--- ",
"K": "-.- ",
"L": ".-.. ",
"M": "-- ",
"N": "-. ",
"O": "--- ",
"P": ".--. ",
"Q": "--.- ",
"R": ".-. ",
"S": "... ",
"T": "- ",
"U": "..- ",
"W": ".-- ",
"X": "-..- ",
"Y": "-.-- ",
"Z": "--.. ",
'1':'.---- ',
'2':'..--- ',
'3':'...-- ',
'4':'....- ',
'5':'..... ',
'6':'-.... ',
'7':'--... ',
'8':'---.. ',
'9':'----. ',}

str = str.replace(/[!,?]/g , '');

str = str.replace(/\s/g, '');

return str.toUpperCase().split("").map(el => {
return morseCode[el] ? morseCode[el] : el;
}).join("").slice(0,-1); // this returns the string without the last character.

};

Remove Last Character From String If It's a ! Using For-Loop - JavaScript

@T.J. Crowder pointed me in the right direction, but he didn't provide an answer that followed my original logic (in this case I wanted to use a for-loop).

The key takeaway is that s = s.replace("!", ""); will work when i-- and s = s.replace(/!+$/g, '') will work when i++. Because, as far as I understand, the replace() method only replaces the first occurrence of the string, which is why we need i-- to force the loop to iterate backwards through the string, making sure that every occurance of "!" gets replaced.

I.e. this will work:

function remove(s) {

for (i = 0; i < s.length; i--) {

let lastChar = s.slice(-1);

if (lastChar === "!") {
s = s.replace("!", '')
}
else {
return s;
}
}
return s;
}

And this will also work:

function remove(s) {

for (i = 0; i < s.length; i++) {

let lastChar = s.slice(-1);

if (lastChar === "!") {
s = s.replace(/!+$/g, '');
}
else {
return s;
}
}
return s;
}

Javascript: Remove last character if a colon

The regular expression literal (/.../) should not be in a string. Correcting your code for removing the colon at the beginning of the string, you get:

myString = myString.replace(/^\:/, '');

To match the colon at the end of the string, put $ after the colon instead of ^ before it:

myString = myString.replace(/\:$/, '');

You can also do it using plain string operations:

if (myString.charAt(myString.length - 1) == ':') {
myString = myString.substr(0, myString.length - 1);
}

How to cut a string at the end delimited by a special character when this character is used multiple times within the same string in JavaScript?

str.slice(0, str.lastIndexOf("/"))

Given that str is hello/world/wide/peace, str.lastIndexOf("/") will find the last occurence of / in str. And then you slice the string from the beginning up to that index, and you take all that and you get hello/world/wide

var str = "hello/world/wide/peace";
console.log(str.slice(0, str.lastIndexOf("/")));

How to remove the first and the last character of a string

Here you go

var yourString = "/installers/";
var result = yourString.substring(1, yourString.length-1);

console.log(result);

remove some last characters

You could use .slice

const input = prompt('Enter something');

alert(input.slice(0, 5))

I want my delete button to remove last character in the string. Substring and slice not working as expected in javascript

numValue = numValue.substring(0, numValue.length -1);


Related Topics



Leave a reply



Submit