Escape HTML <Br> - Java java.lang

Java examples for java.lang:String HTML

Description

Escape HTML <Br>

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String original = "java2s.com";
        System.out.println(HTMLEscapeBr(original));
    }/* w w w  .j a v  a 2  s  .  co  m*/

    public static String HTMLEscapeBr(String original) {
        if (original == null)
            return "";
        StringBuffer out = new StringBuffer("");
        char[] chars = original.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            boolean found = true;
            switch (chars[i]) {
            case '\n':
                out.append("<br/>");
                break; //newline 
            case '\r':
                break;
            default:
                found = false;
                break;
            }
            if (!found)
                out.append(chars[i]);

        }
        return out.toString();
    }
}

Related Tutorials