Skip to content
JsDevLife
Menu
  • Home
  • Leetcode in JS
  • About me
  • Contact Us
Menu

JavaScript check string contains in string or not using search() method

Posted on December 14, 2019 by Vikas Kad

JS find string contains in string

If you want to check whether substring present or not in javascript then following are methods you can use:

JavaScript Search() method to check whether the string contains substring is there or not

const str = “this is main string to check”;
const subStr = “main string”;
if (str.search(subStr)) {
console.log(`${subStr} string is present in ${str}`);
} else {
console.log(`${subStr} string is not present in ${str}`);
}
The Output will be as follows:
JavaScript check string contains in string or not

Definition of Search() method

  • The search() method searches a string for a specified value and returns the position of the match.
  • Search()  method returns -1 if no match is found.
  • We can search on a string or on a regular expression.
  • It will return the first occurrence of the specified value.
I will post more method in upcoming posts which I will link over there so you can check it.

Thanks.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Solving LeetCode Problem 79 – Word Search in JavaScript
  • Solving LeetCode Problem 48: Rotate Image using JavaScript
  • Solving the “Container With Most Water” LeetCode Problem in JavaScript – A Comprehensive Guide
  • LeetCode Solution: 54: Spiral Matrix in JavaScript
  • Solution to LeetCode Problem 31. Next Permutation in JavaScript

Archives

  • 2023 (5)
  • 2022 (20)
  • 2021 (2)
  • 2020 (4)
  • 2019 (14)
  • 2018 (17)

Categories

  • blockchain development
  • Blog
  • crystal
  • flutter
  • flutter.io
  • GitHub
  • Installation
  • Ionic Framework
  • javascript
  • leetcode-in-js
  • masteringInJavasript
  • mcqs
  • MongoDB
  • nodejs
  • Object Oriented Javacript
  • python
  • smart contracts
  • visual studio

Quick Links

  • Home
  • Leetcode in JS
  • About me
  • Contact Us

Terms of service

  • Terms Of Service
  • Disclaimer
©2023 JsDevLife | Design: Newspaperly WordPress Theme