Java中如何用indexOf与for迭代器检查字符串是否包含指定子串?
Hey Alex, no worries at all—let's break this down step by step since you're new to Java. I'll show you exactly how to implement this using both indexOf() and iteration (including explicit Iterator usage) to check your ambient objects.
indexOf() and Iteration I’ll assume your ambient is a collection (like a List) holding 4 objects, and each object has a string value we need to scan. I’ll cover basic string objects first, then show how to adapt this for custom objects if that’s what you’re working with.
Step 1: Basic For Loop + indexOf()
First, here’s a straightforward approach using a standard for loop. Remember: indexOf() returns -1 if the substring isn’t found, so we just check if the result is greater than or equal to 0 to confirm a match.
import java.util.List; public class SubstringChecker { public static void main(String[] args) { // Example ambient with 4 string objects List<String> ambient = List.of("SampleText", "CheckAutonomyTest", "AnotherString", "NoMatchHere"); String targetSubstring = "CheckAutonomy"; // Iterate through each object in ambient for (int i = 0; i < ambient.size(); i++) { String currentString = ambient.get(i); // Use indexOf to check for the substring if (currentString.indexOf(targetSubstring) != -1) { System.out.println("Found '" + targetSubstring + "' in object at index " + i + ": " + currentString); } else { System.out.println("No match in object at index " + i + ": " + currentString); } } } }
Step 2: Using Explicit Iterator Interface
If you specifically need to use the Iterator interface (what some folks refer to as a "for iterator"), here’s that version. We’ll use the iterator to traverse the collection and track the position manually:
import java.util.Iterator; import java.util.List; public class SubstringCheckerWithIterator { public static void main(String[] args) { List<String> ambient = List.of("SampleText", "CheckAutonomyTest", "AnotherString", "NoMatchHere"); String targetSubstring = "CheckAutonomy"; Iterator<String> iterator = ambient.iterator(); int index = 0; // Track position since Iterator doesn't expose indexes directly while (iterator.hasNext()) { String currentString = iterator.next(); if (currentString.indexOf(targetSubstring) != -1) { System.out.println("Found '" + targetSubstring + "' in object at index " + index + ": " + currentString); } index++; } } }
Step 3: Adapting for Custom Objects
If your ambient holds custom objects (not just strings)—say a class like AmbientObject with a method to get the text you need to check—just adjust the code to access that method:
// Your custom ambient object class class AmbientObject { private String content; public AmbientObject(String content) { this.content = content; } public String getContent() { return content; } } // Checker code for custom objects import java.util.List; public class CustomObjectChecker { public static void main(String[] args) { List<AmbientObject> ambient = List.of( new AmbientObject("SampleText"), new AmbientObject("CheckAutonomyTest"), new AmbientObject("AnotherString"), new AmbientObject("NoMatchHere") ); String targetSubstring = "CheckAutonomy"; // Enhanced for loop (uses Iterator under the hood) for (AmbientObject obj : ambient) { String content = obj.getContent(); if (content.indexOf(targetSubstring) != -1) { System.out.println("Found match in object: " + content); } } } }
Quick Tips:
indexOf()is case-sensitive! For case-insensitive checks, convert both the target and current string to lowercase (or uppercase):if (currentString.toLowerCase().indexOf(targetSubstring.toLowerCase()) != -1) { // Case-insensitive match found }- The enhanced for loop (
for (AmbientObject obj : ambient)) is a shorthand that uses an Iterator behind the scenes—so it’s a valid iterator-based approach if you don’t need the index.
I hope this clears things up! Let me know if you need to tweak this for your specific ambient object setup.
内容的提问来源于stack exchange,提问作者Alex Vlad

