Navigate back to the homepage

how to get last two character of string in react js?

Infinitbility
React
August 17th, 2022 · 1 min read
how to get last two character of string in react js?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To get last two character of string in react js, use substring() method to extracts last two character from string. You have to only pass starting and ending postion of string so for last two character pass text.length - 2 as parameter it will return last two character of string.

Let’s see short example to use substring() to get last two character from string.

1text.substring(text.length - 2);

Today, I’m going to show you How do I get last two character of string in react js, as above mentioned, I’m going to use the above-mentioned substring() method.

Let’s start today’s tutorial how do you get last two character of string in react js?

React JS get last two character of string example using substring()

Here, we will take string state with some data and use substring() method to get last two char of string.

1import React, { useState, useEffect } from "react";
2export default function App() {
3 const [str, setStr] = useState("Infinitbility");
4
5 useEffect(() => {
6 setStr(str.substring(str.length - 2))
7 }, []);
8
9 return (
10 <div className="App">
11 <p>{str}</p>
12 </div>
13 );
14}

Output

I hope it helps you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to get last two character of string in react native?

how to get last two character of string in react native?

An example of get last two character of string in react native with the step-by-step guide.

August 17th, 2022 · 1 min read
how to get last two character of string in typescript?

how to get last two character of string in typescript?

An example of get last two character of string in typescript with the step-by-step guide.

August 17th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]