Hi Friends 👋,

Welcome To Infinitbility! ❤️

To remove last character from string in react js, just use substring(0, lastindex - 1) method it will return new string based on the provided index so if you pass 0, lastindex - 1 as parameter it returns the string without last index char.

The substring() method returns the part of the string between the start and end indexes, or to the end of the string.

Let’s take an example to remove the last alphabet using the substring() method.

var str = "infinitbility";

str.substring(0, str.length - 1);
// 👇️ Output
//   "infinitbilit"

Today, I’m going to show you How do I remove last character from string in react js, as above mentioned here, I’m going to use the substring() method to delete last char from string.

Let’s start today’s tutorial how do you remove last character from the string in react js?

In this example, we will do

  1. create a sample string state
  2. used the substring() method to remove the last character
  3. print the output string on the screen

Let’s write code…

react js substring example

import React, { useState, useEffect } from "react";
export default function App() {
    const [str, setStr] = useState("infinitbility");

    return (
        <div className="App">
            <p>{str.substring(0, str.length - 1)}</p>

As mentioned above, we are taken the example of a string state, remove last character from string and print the output in the screen.

I hope it’s help you, All the best 👍.