Navigate back to the homepage

how to get first character of string react js?

Infinitbility
React
June 29th, 2022 · 1 min read
how to get first character of string react js?

Hi 👋,

Welcome To Infinitbility! ❤️

To get the first character of the string to react js, just use charAt(0) it will return the first char of a string.

For the sample let’s see how can we use charAt(0) on a string.

1// 👇️ i
2console.log("infinitbility".charAt(0));

Today, I’m going to show you How do I get the first character of a string in to react js, as above mentioned here, I’m going to use the charAt(0) so we can first alphabet of the string.

Let’s start today’s tutorial how do you get the first character of a string in react js?

In this example, we will do

  1. create a sample string state
  2. use charAt(0) method to get first char
  3. print the first character on a page

Let’s write code…

1import React, { useState, useEffect } from "react";
2import "./styles.css";
3export default function App() {
4 const [str, setStr] = useState("infinitbility.com");
5
6
7 return (
8 <div className="App">
9 <h1>{str.charAt(0)}</h1>
10 </div>
11 );
12}

As mentioned above, we are taken the example of a sample string, used a char at a function to get the first char, and printed the output on a screen.

Let’s check the output.

I hope it’s help 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 query string value in react js?

how to get query string value in react js?

An example of get query string value in react js with step by step guide.

June 29th, 2022 · 1 min read
Get first letter of each word in a string in react js

Get first letter of each word in a string in react js

An example of get first letter of every word from string in react js.

June 28th, 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]