Navigate back to the homepage

how to check if string contains substring in react js?

Infinitbility
React
July 5th, 2022 · 1 min read
how to check if string contains substring in react js?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To check the string that contains a substring in react js, use the includes() method, it will return true or false based on string contains a substring or not.

In the following example, we will take the samples string and substring, and check the string contains a substring using the includes() method.

1let string = "hi friends welcome to infinitbility";
2let substring = "infinitbility";
3
4string.includes(substring); // true

Today, I’m going to show you How do i check the string containing substring in react js, as mentioned above here, I’m going to use the includes() method.

Let’s start today’s tutorial how do you check the string containing substring in react js?

In this example, we will do

Final Result: Search filter in React
  1. take an example of a string state
  2. use the includes() methods
  3. check string contains the substring
  4. print the output on the page screen
1import React, { useState } from "react";
2export default function App() {
3 const [sampleString, setSampleString] = useState("hi friends welcome to infinitbility");
4
5 return (
6 <div className="App">
7 <h1>{`Example of check string contains substring`}</h1>
8 <p>{sampleString.includes("infinitbility").toString()}</p>
9 </div>
10 );
11}

Note: I have used the toString() method to only show the output on the screen

In the above react js example, we have taken the sample string state, performed an example of a check string containing substring, and printed the output on the screen.

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 generate random number in react js?

how to generate random number in react js?

An example of generate random number in react js with a step-by-step example.

July 5th, 2022 · 1 min read
how to add two numbers in react js?

how to add two numbers in react js?

An example of add two numbers in react js with a step-by-step example.

July 4th, 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]