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 Javascript.

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 Javascript With Example Code

Java String contains() method is used to check whether a particular sequence of characters is present in a given string or not. In JavaScript, we can use the includes() method to achieve the same functionality.

The includes() method is a built-in method in JavaScript that returns true if the specified string is found in the given string, otherwise it returns false. The syntax of the includes() method is as follows:

string.includes(searchString, position)

Here, the searchString parameter is the string that we want to search for in the given string, and the optional position parameter is the position in the string where we want to start the search.

Let’s take an example to understand this better:

const str = 'Hello, World!';

console.log(str.includes('Hello')); // true

console.log(str.includes('world')); // false

In the above example, we have defined a string ‘Hello, World!’ and used the includes() method to check whether the string ‘Hello’ is present in it or not. The first console.log() statement returns true because the string ‘Hello’ is present in the given string, whereas the second console.log() statement returns false because the string ‘world’ (with a lowercase ‘w’) is not present in the given string.

In conclusion, we can use the includes() method in JavaScript to achieve the same functionality as the Java String contains() method.

Equivalent of Java String contains in Javascript

In conclusion, the equivalent function of Java’s String contains() method in JavaScript is the includes() method. Both methods are used to check if a string contains a specific substring. However, the syntax and usage of these methods differ slightly. While the contains() method takes a CharSequence as an argument, the includes() method takes a string as an argument. Additionally, the includes() method returns a boolean value indicating whether the substring is present in the string or not. Overall, the includes() method is a powerful tool in JavaScript that can be used to efficiently search for substrings in a string.

Contact Us