how to see if a string contains word in a if statement code example
Example 1: How do I check if a string contains a specific word?
$a = 'Hello world?';
if (strpos($a, 'Hello') !== false) { //PAY ATTENTION TO !==, not !=
echo 'true';
}
if (stripos($a, 'HELLO') !== false) { //Case insensitive
echo 'true';
}
Example 2: how to use the if sentence in the string
String str = "Game of Thrones";
//This will print "true" because "Game" is present in the given String
System.out.println(str.contains("Game"));
/* This will print "false" because "aGme" is not present, the characters
* must be present in the same sequence as specified in the contains method
*/
System.out.println(str.contains("aGme"));