質問

I am using the following code to extract all alt tags from an html file:

Elements imgs =  doc.select("img");

                    for (Element alts : imgs) {

                        String altText = alts.attr("alt");


                        if(!(alts.attr("alt").equals(null)))
                            System.out.println("alt tags: " + alts.attr("alt"));
                        //getGui().setTextVers("\r\n"  + ">\r\n" + altText + "\r\n" );

                    }

I would like to ignore the alt tags which are blank and just print out the tags that contain text. I am using an if statement to test for empty alt tags but it still prints out all alt tags even though some contain text. Could someone help please?

Thanks

役に立ちましたか?

解決

Maybe change your if statement to be:

if ((altText != null) && !altText.isEmpty())
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top