The Java String contains function is a method that is used to check whether a particular sequence of characters is present in a given string or not. It returns a boolean value of true if the specified sequence of characters is found in the string, and false otherwise. The method takes a single argument, which is the sequence of characters to be searched for in the string. It is case-sensitive, meaning that it will only match the exact sequence of characters provided. The contains function is commonly used in string manipulation and searching operations in Java programming. Keep reading below to learn how to Java String contains in PHP.

Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!

Buy Now On Amazon

Java String contains in PHP With Example Code

Java String contains is a useful method that allows you to check if a specific string is present within another string. In PHP, you can achieve the same functionality using the strpos() function.

The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found, it returns false. Here’s an example:


$string = "Hello, world!";
$substring = "world";

if (strpos($string, $substring) !== false) {
echo "The string '$substring' was found in the string '$string'";
} else {
echo "The string '$substring' was not found in the string '$string'";
}

In this example, we’re checking if the substring “world” is present in the string “Hello, world!”. Since it is, the output will be “The string ‘world’ was found in the string ‘Hello, world!'”.

It’s important to note that strpos() is case-sensitive. If you want to perform a case-insensitive search, you can use the stripos() function instead.


$string = "Hello, world!";
$substring = "WORLD";

if (stripos($string, $substring) !== false) {
echo "The string '$substring' was found in the string '$string'";
} else {
echo "The string '$substring' was not found in the string '$string'";
}

In this example, we’re performing a case-insensitive search for the substring “WORLD” in the string “Hello, world!”. Since it is present (even though the case doesn’t match), the output will be “The string ‘WORLD’ was found in the string ‘Hello, world!'”.

Equivalent of Java String contains in PHP

In conclusion, the equivalent function of Java’s String contains() in PHP is the strpos() function. Both functions serve the same purpose of checking if a string contains a specific substring. However, there are some differences in their syntax and usage. While the contains() function in Java returns a boolean value, the strpos() function in PHP returns the position of the first occurrence of the substring. Additionally, the strpos() function is case-sensitive by default, but this can be changed by using the stripos() function instead. Overall, understanding the equivalent function in PHP can be helpful for developers who are familiar with Java and need to work with PHP.

Contact Us