Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a string has a certain piece of text [duplicate]

People also ask

How do you check if a string contains a specific substring?

You can use contains(), indexOf() and lastIndexOf() method to check if one String contains another String in Java or not. If a String contains another String then it's known as a substring. The indexOf() method accepts a String and returns the starting position of the string if it exists, otherwise, it will return -1.

How do I check if a string contains a specific word in JavaScript?

The includes() method returns true if a string contains a specified string. Otherwise it returns false .

How do you check if a string contains a substring TypeScript?

Check if a String Has a Certain Text Using the indexOf() Method in TypeScript. The indexOf() method is used to check whether a string contains another string or not.


Here you go: ES5

var test = 'Hello World';
if( test.indexOf('World') >= 0){
  // Found world
}

With ES6 best way would be to use includes function to test if the string contains the looking work.

const test = 'Hello World';
if (test.includes('World')) { 
  // Found world
}