-1

I have a Java string like this:

String str = "<table><tr><td>ALL,</td></tr><tr><td></td></tr><tr><td> Please find attached file for this week<tr><td></td></tr><tr><td>Thanks</td></tr><tr><td>Support Team</td></tr>";

I want output like this:

All,

Please find attached file for this week.

Thanks, Support Team

Rims
  • 78
  • 2
  • 9
  • 1
    That's not going to be easy as the html is not well-formed. Unless you use regexes, which is not recommendable – g00se Jun 22 '23 at 09:55
  • you should provide more context to the question. Do you need to format the string with carriage return (\n)? Giving you are dealing with a I think you are looking for a templating engine such as FreeMarker or Thymeleaf.
    – tonnoz Jun 22 '23 at 09:56
  • Does this answer your question? [how to convert HTML text to plain text?](https://stackoverflow.com/questions/3607965/how-to-convert-html-text-to-plain-text) – fantaghirocco Jun 22 '23 at 10:55
  • Does this answer your question? [Remove HTML tags from a String](https://stackoverflow.com/questions/240546/remove-html-tags-from-a-string) – Jorn Jun 22 '23 at 11:24

1 Answers1

2

You should really use a proper html parser, but if you want something quick and dirty and your html is well-formed you can use something from package javax.swing.text.html:

    public static String stripTags(String content) throws Exception {
        String result = null;
        HTMLEditorKit kit = new HTMLEditorKit();
        InputStream in = new ByteArrayInputStream(content.getBytes());
        Document doc = new HTMLDocument();
        kit.read(in, doc, 0);
        result = doc.getText(0, doc.getLength());

        return result;
    }
g00se
  • 3,207
  • 2
  • 5
  • 9