I'm assuming the problems you're having with indexOf() related to you using the character version (otherwise why would you be searching for w when looking for world?). If so, indexOf() can take a string argument to search for:

String s = "hello world i am from heaven";
if (s.indexOf("world") != -1) {
  // it contains world
}

as for log base 2, that's easy:

public static double log2(double d) {
  return Math.log(d) / Math.log(2.0d);
}
Answer from cletus on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ searching-for-characters-and-substring-in-a-string-in-java
Searching For Characters and Substring in a String in Java - GeeksforGeeks
July 23, 2025 - The indexOf() searchesfor the first occurrence of a character from the beginning of the string and returns its index. If the character is not found, it returns -1. Note: If the given string contains multiple occurrences of a specified character ...
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_string_indexof.asp
Java String indexOf() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ยท โฎ String Methods ยท Search a string for the first occurrence of "planet": String myStr = "Hello planet earth, you are a great planet."; System.out.println(myStr.indexOf("planet")); Try it Yourself ยป ยท
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ javaexamples โ€บ string_search.htm
How to Search a Word inside a String in Java
This example shows how we can search a word within a String object using indexOf() method which returns a position index of a word within the string if found.
๐ŸŒ
Java Guides
javaguides.net โ€บ 2018 โ€บ 08 โ€บ string-searching-methods-with-examples.html
String Searching Methods in Java
June 15, 2024 - The matches() method allows you to check if a string matches a given regular expression. Understanding these methods will help you manipulate and analyze strings more effectively in Java.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ searching-for-character-and-substring-in-a-string
Searching characters and substring in a String in Java - GeeksforGeeks
December 11, 2018 - contains(CharSequence seq): It returns true if the String contains the specified sequence of char values otherwise returns false. Its parameters specify sequence of character to be searched and throws NullPointer exception if seq is null.
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2016 โ€บ 10 โ€บ how-to-check-if-string-contains-another-substring-in-java-indexof-example.html
How to check if String contains another SubString in Java? contains() and indexOf() example
June 26, 2025 - YES Doing search with different case isPresent: false Checking if one String contains another String using contains() in Java input string: C++ is the predecessor of Java search string: Java does substring is found inside String?
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java list โ€บ searching for a string in an arraylist
Searching for a String in an ArrayList | Baeldung
April 4, 2025 - public List<String> findUsingLoop(String search, List<String> list) { List<String> matches = new ArrayList<String>(); for(String str: list) { if (str.contains(search)) { matches.add(str); } } return matches; } The Java 8 Streams API provides us with a more compact solution by using functional operations.
Find elsewhere
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ java-how-to-find-a-word-in-a-java-string-using-the-indexof-method-414025
How to find a word in a Java String using the indexOf() method | LabEx
Try running the program again with different search words to see how it performs with various inputs. In this tutorial, you have learned how to use the indexOf() method in Java to find words within strings.
๐ŸŒ
Amygdalum
stringsearchalgorithms.amygdalum.net
StringSearchAlgorithms - Efficient String Search Algorithms in Java
StringSearchAlgorithms provides a Java library of various algorithms to support you in string searching - supporting single patterns and multi patterns.
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ lang โ€บ String.html
String (Java Platform SE 8 )
July 21, 2026 - The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the Character class. The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ string search algorithms for large texts with java
String Search Algorithms for Large Texts with Java
January 8, 2024 - Worst-case scenario occurs in the case of a String having many partial occurrences: Text: baeldunbaeldunbaeldunbaeldun Pattern: baeldung ยท As mentioned above, Simple Text Search algorithm is very inefficient when patterns are long and when there is a lot of repeated elements of the pattern.
๐ŸŒ
IIT Kanpur
iitk.ac.in โ€บ esc101 โ€บ 05Aug โ€บ tutorial โ€บ java โ€บ data โ€บ searchar.html
Searching for a Character or a Substring within a String
The String class provides two accessor methods that return the position within the string of a specific character or substring: indexOf and lastIndexOf. The indexOf method searches forward from the beginning of the string, and lastIndexOf searches backward from the end of the string.
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ checking-if-a-string-contains-a-word-or-phrase-in-java-08ef51495d87
Checking if a String Contains a Word or Phrase in Java
June 16, 2025 - The other is built into Javaโ€™s regular expression tools. The cleanest way to level the field is to convert both strings to lowercase or uppercase. That way, case stops being part of the equation. You just compare letters without case getting in the way. String message = "Scheduled System Check"; String search = "system"; boolean match = message.toLowerCase(Locale.ROOT).contains(search.toLowerCase(Locale.ROOT)); // true
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ java โ€บ standard library โ€บ java โ€บ lang โ€บ string โ€บ indexof()
Java String indexOf() - Find Character Index | Vultr Docs
December 17, 2024 - The indexOf() method in Java is a powerful tool used to find the position of a specific character or substring within a string. This function returns the index within the calling String object of the first occurrence of the specified value, ...
๐ŸŒ
Amygdalum
amygdalum.net โ€บ en โ€บ text-search-java.html
String Search in Java (using the JDK-API)
October 10, 2016 - Java uses a naive string searching algorithm, to determine the position of a match. Naive means that the text is processed character by character, looking for a match with the first character of the pattern. After encountering the first character match a new subroutine is started, comparing ...
๐ŸŒ
YouTube
youtube.com โ€บ watch
Lesson 13 - Java Tutorial - Searching A String For A Substring - YouTube
This is just a few minutes of a complete course. Get full lessons & more subjects at: http://www.MathTutorDVD.com.
Published: August 10, 2016
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-string-contains-method-example
Java String contains() Method with Example - GeeksforGeeks
July 11, 2025 - In this example, we check if a specific substring is present in the given string. ... // Java Program to demonstrate working contains() method class Gfg { public static void main(String args[]) { String s = "My name is GFG"; System.out.println(s.contains("GFG")); System.out.println(s.contains("geeks")); } }