How to get java substring ignoring \r\n and few html tags

Viewed 27

Original String

\r\n HDFC Bank <\/a>\r\n <\/div>\r\n <\/td>\r\n

Required sub-string

HDFC Bank
1 Answers

You can use the JSoup library to parse HTML content, and extract content text from it.

With JSoup, you parse an input content to a "Document" structure that contains HTML "Elements". On each Jsoup "Element" or "Document", you will find a text() method that allow to extract and trim text content from parsed HTML elements:

Gets the normalized, combined text of this element and all its children. Whitespace is normalized and trimmed.

[...]

Note that this method returns the textual content that would be presented to a reader.

Example:

import org.jsoup.Jsoup;

public class JsoupGetText {

    public static void main(String[] args) {
        var txt = "\r\n HDFC Bank </a>\r\n </div>\r\n </td>\r\n";

        var extracted = Jsoup.parse(txt).text();
        System.out.println('\''+extracted+'\'');
    }
}

prints:

'HDFC Bank'
Related