
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
In how many ways can we find a substring inside a string in javascript?
We can find a substring inside a string in two ways. One way is using the indexOf() method and the other is using ES6 includes() method. let's discuss them in detail.
indexOf()
syntax
indexOf(str);
This method tries to check the index of the substring we need. If there is index, which means substring is present, then true will be displayed in the output else false will be displayed as output. This method is case sensitive.
Example
<html> <body> <script> var company = "Tutorix"; document.write(company.indexOf('Tutor') !== -1); document.write("</br>"); document.write(company.indexOf('tutor') !== -1); </script> </body> </html>
Output
true false
includes()
syntax
includes(str);
Unlike the indexOf() method, this method will check the string we provided whether it is present or not. If present then true will be displayed as output else false will be displayed as output. This method is also case sensitive. We need to provide an exact string to check its presence.
Example
<html> <body> <script> var company = "tutorialspoint"; document.write(company.includes('Tutor')); document.write("</br>"); document.write(company.includes('point')); </script> </body> </html>
Output
false true
- Related Articles
- In how many ways can we split a string in JavaScript?
- In how many ways we can convert a String to a character array using Java?
- How can we extract a substring from a string in MySQL?
- How can we get substring from a string in Python?
- How many ways a String object can be created in java?
- Program to find how many ways we can climb stairs in Python
- In how many ways we can concatenate Strings in Java?
- How to extract a substring from inside a string in Python?
- How can we replace all the occurrences of a substring with another substring within a string in MySQL?
- In MySQL, how can we insert a substring at the specified position in a string?
- How many ways can a property of a JavaScript object be accessed?
- How many ways can we read data from the keyboard in Java?
- Program to check how many ways we can choose empty cells of a matrix in python
- What are JSP declarations? In how many ways we can write JSP declarations?
- How to find a substring from a string in C#?

Advertisements