I am trying to create a method to find and return the first tag in a given HTML string, and returns null if no such tag is found. (A tag would be something like <b>
)
I looked through the String class methods but I can't find a method that can suit this purpose. I'm thinking my plan is to scan each word for a "<" then once it is found, scan for a ">", but am unsure of how to do so. Also wondering if I should put a while/for loop in there? Help is appreciated, thank you.
public class HTMLProcessor {
public static void main(String[] args) {
System.out.println(findFirstTag("<b>The man jumped.</b>"));
}
public static String findFirstTag(String text) {
int firstIndex = text.indexOf("<");
if (firstIndex >= 0) {
String newText = text.substring(firstIndex);
int secondIndex = newText.indexOf(">");
return text.substring(firstIndex, secondIndex + 1);
} else {
return null;
}
}