Java HTML Encode forHTML(final String aText)

Here you can find the source of forHTML(final String aText)

Description

Encodes the string to be used in HTML.

License

Open Source License

Parameter

Parameter Description
aText string.

Return

encoded.

Declaration

public static String forHTML(final String aText) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;

public class Main {
    /**// w w  w.  j a  va  2s  . co m
     * Encodes the string to be used in HTML.
     * 
     * @param aText string.
     * @return encoded.
     */
    public static String forHTML(final String aText) {
        final StringBuilder result = new StringBuilder();
        final StringCharacterIterator iterator = new StringCharacterIterator(aText);
        char character = iterator.current();
        while (character != CharacterIterator.DONE) {
            if (character == '\"') {
                result.append("DOUBLE_QUOTE");
            } else if (character == '\r') {
                result.append("RETURN_CAR");
            } else if (character == '\n') {
                result.append("NEW_LINE");
            } else if (character == '\'') {
                result.append("SINGLE_QUOTE");
            } else {
                // the char is not a special one
                // add it to the result as is
                result.append(character);
            }
            character = iterator.next();
        }
        return result.toString();
    }
}

Related

  1. encodeHTML(String aText)
  2. encodeHTML(String s)
  3. forHTML(String aText)
  4. forHtmlComplete(final String aText)
  5. forHTMLTag(String aTagFragment)
  6. forHTMLTag(String aTagFragment)