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.

let string = "hi friends welcome to infinitbility";
let substring = "infinitbility";

string.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

  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
import React, { useState } from "react";
export default function App() {
  const [sampleString, setSampleString] = useState("hi friends welcome to infinitbility");

  return (
    <div className="App">
      <h1>{`Example of check string contains substring`}</h1>
      <p>{sampleString.includes("infinitbility").toString()}</p>
    </div>
  );
}

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 đź‘Ť.