سؤال

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