Search code examples
javaandroidhtmltagsjsoup

How can i separate a specific tag in java


I have this html:

<p dir="ltr">Hello<br>
<img src="/path"> <br>
<b>hey</b> <b>hello</b> fox<br>
cat</p>

how can i separate the img tag from p? I want it to be like this:

<p dir="ltr">Hello<br>
<br>
<b>hey</b> <b>hello</b> fox<br>cat</p>
<img src="/path"> 

Solution

  • You can use indexOf('<img'), you will get the Start index

    Then use indexOf('>'), you will get the index of End tag

    Suppose you have the string like

    String html = "<p dir="ltr">Hello<br><img src="path"> <br><b>hey</b> <b>hello</b> fox<br>cat</p>";
    String image = html.substring(html.indexOf("<img"), html.indexOf(">"));
    

    Then you know what to do!!

    Inform me if you need anything else