Java Utililty Methods HTML

List of utility methods to do HTML

Description

The list of methods to do HTML are organized into topic(s).

Method

Stringhtmlify(String str)
Turns a regular string into a form appropriate for a HTML document.
str = str.replace("&", "&");
str = str.replace("<", "&lt;");
str = str.replace(">", "&gt;");
return str;
StringhtmlItemName(int itemId)
Method htmlItemName.
return "&#" + itemId + ";";
StringhtmlLineBreaks(String s)
Convert newlines in the string into <br/> tags.
if (s == null) {
    return "";
StringBuilder out = new StringBuilder();
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
    if (chars[i] == '\n') {
        out.append("<br />");
...
StringhtmlLink(String url, String linkName)
html Link
return new StringBuffer().append("<a href=\"").append(url).append("\">").append(linkName).append("</a>")
        .toString();
StringhtmlNewline(String text)
Replaces common newline characters like \n, \r, \r\n to the HTML line break tag br.
if (text == null || text.trim().isEmpty()) {
    return null;
return text.replaceAll("(\n|\r|\r\n)", "<br>");
StringhtmlSafe(String field)
Make string safe to output as value="" in an html form
if (field == null) {
    return "";
field = field.replaceAll("\"", "&quot;");
field = field.replaceAll("<", "&lt;");
field = field.replaceAll(">", "&gt;");
return field;
StringhtmlSafe(String value)
html Safe
value = value.replace("&", "&amp;"); 
value = value.replace("<", "&lt;");
value = value.replace(">", "&gt;");
value = value.replace("\"", "&quot;");
return value;
inthtmlSize(String str)
retreive the size of a String with html code.
int c = 0;
int cInCode = 0;
boolean inHTMLCode = false;
for (int i = 0; i < str.length(); i++) {
    if (!inHTMLCode) {
        if (str.charAt(i) == '&') {
            inHTMLCode = true;
            cInCode++;
...
StringhtmlString(String str)
html String
str = str.replaceAll("\\&", "&amp;");
str = str.replaceAll("\\<", "&lt;");
str = str.replaceAll("\\>", "&gt;");
return "<html>" + str;
StringhtmlText(String text)
This method converts texts to be displayed on html-page.
StringBuilder sb = new StringBuilder();
char c;
if (text == null)
    return "";
for (int i = 0; i < text.length(); i++) {
    c = text.charAt(i);
    switch (c) {
    case '<':
...