W3Schools
w3schools.com › java › ref_string_indexof.asp
Java String indexOf() Method
String myStr = "Hello planet earth, you are a great planet."; System.out.println(myStr.indexOf("planet")); ... The indexOf() method returns the position of the first occurrence of specified character(s) in a string.
Stack Overflow
stackoverflow.com › questions › 46881080 › how-to-find-the-index-of-first-occurence-of-any-character-from-another-string-in
java - How to find the index of first occurence of any character from another string in a string? - Stack Overflow
You can use String.indexOf method which is present under string class.
AlgoMonster
algo.monster › liteproblems › 28
28. Find the Index of the First Occurrence in a String - In-Depth Explanation
In-depth solution and explanation for LeetCode 28. Find the Index of the First Occurrence in a String in Python, Java, C++ and more. Intuitions, example walk through, and complexity analysis. Better than official and forum solutions.
GitHub
github.com › doocs › leetcode › blob › main › solution › 0000-0099 › 0028.Find the Index of the First Occurrence in a String › README_EN.md
leetcode/solution/0000-0099/0028.Find the Index of the First Occurrence in a String/README_EN.md at main · doocs/leetcode
Input: haystack = "sadbutsad", needle = "sad" Output: 0 Explanation: "sad" occurs at index 0 and 6. The first occurrence is at index 0, so we return 0. ... Input: haystack = "leetcode", needle = "leeto" Output: -1 Explanation: "leeto" did not ...
Author doocs
TutorialKart
tutorialkart.com › java › get-the-index-of-first-occurrence-of-a-substring-in-a-string-using-java
How to get the index of first occurrence of a substring in a String using Java ?
May 4, 2023 - In this Java tutorial, you will learn how to find the index of first occurrence of a search string in a given string using String.indexOf() function, with examples.
WalkCCC
walkccc.me › LeetCode › problems › 28
28. Find the Index of the First Occurrence in a String - LeetCode Solutions
LeetCode Solutions in C++23, Java, Python, MySQL, and TypeScript.
Substack
alexanderobregon.substack.com › p › leetcode-28-find-the-index-of-the
LeetCode #28: Find the Index of the First Occurrence in a String — Solved in Java
October 24, 2025 - Thinking through a solution in Java starts with how string comparison works at the character level. Every character can be read directly with charAt, which lets you match needle against slices of haystack without creating new strings. The simplest way is to slide one index through the text and check for a match at every position, which helps build an understanding of how pattern matching works mechanically.
Top answer 1 of 4
9
After you've found the first index, use the overloaded version of indexOf that receives the start index as a second parameter:
public int indexOf(int ch, int fromIndex)Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
Keep doing that until indexOf returns -1, indicating that there are no more matches to be found.
2 of 4
4
You can use regex with Pattern and Matcher. Matcher.find() tries to find the next match and Matcher.start() will give you the start index of the match.
Pattern p = Pattern.compile("create");
Matcher m = p.matcher("I would like to create a book reader have create, create ");
while(m.find()) {
System.out.println(m.start());
}
Baeldung
baeldung.com › home › java › java string › java string.indexof()
Java.String.indexOf() | Baeldung
April 11, 2025 - • Java String.indexOf() (current article)• Java String.intern() • Java String.isEmpty() • Java String.lastIndexOf() • Java String.regionMatches() • Java String.replace() • Java String.replaceAll() • Java String.split() • Java String.startsWith() • Java String.subSequence() • Java String.substring() • Java String.toLowerCase() • Java String.toUpperCase() • Java String.trim() • Java String.valueOf() The method indexOf() ...
GitHub
github.com › LeetCode-Feedback › LeetCode-Feedback › issues › 24614
Missing Test Case - 28. Find the Index of the First Occurrence in a String · Issue #24614 · LeetCode-Feedback/LeetCode-Feedback
October 5, 2024 - Find the Index of the First Occurrence in a String#24614 ... take input of sub-string here depicted as "needle" take input of super-string here depicted as "haystack" check index of sub-string the super_string use indexOf function ... import ...
Published Oct 05, 2024
TutorialsPoint
tutorialspoint.com › get-the-index-of-the-first-occurrence-of-a-separator-in-java
Get the index of the first occurrence of a separator in Java
public class Demo { public static ...Separator's first occurrence: "+sepPos); } } String: Tom-Hank-s Separator's first occurrence: 3 ... PHP – iconv_strpos() function – Find the position of the first occurrence of a needle in a haystack...