Here you can find the source of HtmlEncode(String strInput)
public static String HtmlEncode(String strInput)
//package com.java2s; //License from project: Open Source License public class Main { public static String HtmlEncode(String strInput) { String input = strInput;/*from w ww . jav a 2s. c o m*/ input = nullToEmpty(input); if ("".equals(input)) { return " "; } else { input = Replace(input, "\"", """); input = Replace(input, "<", "<"); input = Replace(input, ">", ">"); input = Replace(input, " ", " "); input = Replace(input, "\r\n", "<br>"); input = Replace(input, "\r", "<br>"); input = Replace(input, "\n", "<br>"); return input; } } public static String nullToEmpty(String str) { return (nullToEmpty(str, "")); } public static String nullToEmpty(String str, String defaultStr) { if (str == null || str == "") return defaultStr; else return str; } public static String nullToEmpty(Object obj) { if (obj == null) return ""; else return obj.toString(); } public static boolean equals(String str1, String str2) { if (str1 == null) { return str2 == null; } return str1.equals(str2); } public static String Replace(String str, String substr, String substitute) { str = nullToEmpty(str); String retstr = str; substr = nullToEmpty(substr); substitute = nullToEmpty(substitute); int substrlen = substr.length(); for (int pos = retstr.indexOf(substr); pos != -1;) { String temp = retstr.substring(0, pos); temp = nullToEmpty(temp); temp = String.valueOf(temp) + String.valueOf(substitute); pos += substrlen; String half = retstr.substring(pos); half = nullToEmpty(half); if ("".equals(half)) { retstr = temp; break; } if (half.indexOf(substr) == -1) pos = -1; else pos = half.indexOf(substr) + temp.length(); retstr = String.valueOf(temp) + String.valueOf(half); } return retstr; } }