Search code examples
javajsoup

jsoup / Java - how to append a nonbreaking space


Calling [Element].appendtext(" ") causes the ampersand to be escaped and so the user sees the ampersand on the rendered HTML page. I don't want the page to render the HTML code.

I actually do want the html page to render whitespace.


Solution

  • A non-breaking space can be written into a string literal using a Unicode escape sequence: "\u00A0".

    However, if you want all whitespace within an element to be rendered literally, consider using normal spaces but applying a CSS property such as white-space: pre;.