1

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.

1
  • 1
    val.slice(0, -1);
    – Naveen
    Commented Nov 10, 2022 at 8:47

2 Answers 2

1

Since React is javascript, you can use substring:

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

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);