You could check the string to see if it contains substrings that look like HTML tags:
// Check if a string contains HTML-like '<[/]abc[/]>' substrings
public static boolean containsHtmlTags(String s)
{
boolean hasTags = false;
int sLen = s.length();
int p = 0;
// Look for '<[/]abc[/]>' substrings
while (p < sLen)
{
// Check for the next '<[/]abc[/]>' substring
boolean hasTag = false;
p = s.indexOf('<', p);
if (p < 0)
break;
p++;
if (p < sLen && s.charAt(p) == '/')
p++;
while (p < sLen)
{
char ch = s.charAt(p);
if (!Character.isLetter(ch))
break;
hasTag = true;
p++;
}
if (p < sLen && s.charAt(p) == '/')
p++;
if (p >= sLen || s.charAt(p) != '>')
hasTag = false;
p++;
hasTags = (hasTags || hasTag);
}
// True if s contains one or more '<[/]abc[/]>' substrings
return hasTags;
}
This is not perfect, but it looks for substrings within a string that look like HTML element tags like <foo>
, </foo>
, or <foo/>
. If the string contains at least such substring one, then the method returns true.
Note that this is a very simple scanner; it does not check for HTML attributes or spaces within tags, or matching opening and closing tag names. For that level of sophistication, you would be better off just using regular expressions or an HTML parser.