Search code examples
javajsoup

How to pass String variable (s_code) to Jsoup pseudo selector " :containsOwn(text) " instead of String value at text


I am trying to get parent row of a column whose value matches the string i stored in variable s_code, from a table in a HTML page

doc = Jsoup.connect(url)
            .data("selZone", z_code)
            .data("selDivision", d_code)
            .data("divisionWise", "DivisionWise")
            .post();
Element column=doc.selectFirst("td:containsOwn(s_code)"); 
                                              //s_code is a String variable
Element parentRow=column.parent();
Elements columns=parentRow.select("td");

Solution

  • If you want the value of the variable s_code replace "s_code" in Element column=doc.selectFirst("td:containsOwn(s_code)"); You can use String.format as below.

    Element column=doc.selectFirst(String.format("td:containsOwn(%s)",s_code));
    

    This will replace %s with the value of s_code