The JavaScript String search() function is used to search for a specified string or regular expression within a given string. It returns the index of the first occurrence of the specified search value, or -1 if the search value is not found. The search() function can take a regular expression as its argument, allowing for more complex search patterns. It is a useful tool for finding specific substrings within a larger string and can be used in a variety of applications, such as form validation or data processing. Keep reading below to learn how to Javascript String search in Java.

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

Javascript String search in Java With Example Code

JavaScript String search() method is used to search a string for a specified value and returns the position of the match. In Java, we can use the indexOf() method to achieve the same functionality.

The indexOf() method returns the index within the calling string of the first occurrence of the specified value, starting the search at fromIndex. If no such value exists, then -1 is returned.

Here is an example code snippet to demonstrate the usage of the indexOf() method:


String str = "Hello World";
int index = str.indexOf("World");
System.out.println("Index of World in Hello World is: " + index);

In the above code, we have a string “Hello World” and we are searching for the index of the substring “World”. The indexOf() method returns the index of the first occurrence of the substring, which is 6 in this case.

We can also specify the starting index from where the search should begin. For example:


String str = "Hello World";
int index = str.indexOf("o", 5);
System.out.println("Index of o in Hello World starting from index 5 is: " + index);

In the above code, we are searching for the index of the character “o” in the string “Hello World” starting from index 5. The indexOf() method returns the index of the first occurrence of the character “o” after index 5, which is 7 in this case.

In conclusion, the indexOf() method in Java can be used to search for a substring or character in a string and returns the index of the first occurrence of the specified value.

Equivalent of Javascript String search in Java

In conclusion, the Java programming language provides a powerful and efficient String search function that is equivalent to the Javascript String search function. The Java String class provides several methods for searching and manipulating strings, including the indexOf() and lastIndexOf() methods, which can be used to search for a specific substring within a larger string. Additionally, the Java Pattern and Matcher classes provide more advanced regular expression-based search capabilities. By leveraging these tools, Java developers can easily search and manipulate strings in their applications with ease and efficiency. Overall, the Java String search function is a valuable tool for any developer working with strings in their Java applications.

Contact Us