Output:- other is not included in string : We wish you a merry christmas We will be checking if the substring ‘merry’ is contained in the string ‘We wish you a merry christmas’ let mainString = "We wish you a merry christmas" fromIndexPosition: The indexOf() method will search for the particular substring staring with fromIndexPosition.substringToBeSearched: is the substring to be searched. IndexOf(substringToBeSearched, fromIndexPosition) In Javascript indexOf() method is used to return the index of the first occurrence of specified string within a string object from where it is called. The example provided shows how the method is used and what will happen depending on whether or not the substring is present.Javascript check if string contains substring using indexOf() The `includes()` method can be used to check if a string contains a certain substring in JavaScript. Otherwise, it will execute the second block of code and log ‘The substring is not present.’ Conclusion The `if` statement will then execute the first block of code, logging ‘The substring is present.’ to the console. In this example, `mainString.includes(substring)` will return `true` if the `mainString` contains the specified `substring`. Here’s an example:Ĭonst mainString = 'Hello, welcome to the world of programming.' Ĭonsole.log('The substring is present.') Ĭonsole.log('The substring is not present.') You can use the `includes()` method to check if a string contains a certain substring in JavaScript. In this blog post, we will look at how to use the `includes()` method with an example. JavaScript provides the `includes()` method to make this process easier and more efficient. Checking if a string contains a certain substring is an important task in programming.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |