Home > OS >  how to remove last letter from string in react js
how to remove last letter from string in react js

Time:11-10

I am trying to remove last character from a string in react

val = "string"

const newVal = value.split("");

i tried this but this didn't helped.

CodePudding user response:

try this

const newVal = value.slice(0, -1);

CodePudding user response:

Since React is javascript, you can use substring:

const newVal = val.substring(0, val.length - 1);

CodePudding user response:

When you try to "delete" the last character, it means that you want to get the character (0, length - 1).

**(method) String.substring(start: number, end?: number | undefined): string

Returns the substring at the specified location within a String object.**

So it's will working:

val = "string";
// const newVal = value.split("");
const newVal = val.substring(0, val.length-1);
  • Related