Rafael
0
Q:

remove first and last character from string javascript

var str = "Your string"
str = str.slice(0, -1); 
console.log(str)
//returns "Your strin"
13
let str = " hello";
str = str.substring(1);
4
const text = 'abcdef'
const editedText = text.slice(0, -1) //'abcde'
0
// Hey buddy, here are different implementation, choose your favourite !!

// best implementation
const removeChar = (str) => str.slice(1, -1);

// regex
function removeChar(str) {
  return str.replace(/^.|.$/g, "");
}

// without slice
function removeChar(str) {
  const array = str.split("");
  let res = "";
  for (let i = 1; i < array.length - 1; i++) res += array[i];
  return res;
}
1
let str = "12345.00";
str = str.substring(0, str.length - 1);
console.log(str);
0
var string = "abcd";
console.log(string.substring(3)); //"d"
1
const removeChar = (str) => str.slice(1, -1);
2
var oldStr ="Hello";
var newStr = oldStr.substring(1); //remove first character "ello"
3
var newStr = str.substring(1);
3
str = str.substring(1);
1

New to Communities?

Join the community