Here you can find the source of htmlEncode(String str)
public static String htmlEncode(String str)
//package com.java2s; //License from project: Apache License public class Main { public static String htmlEncode(String str) { if (str == null) { return ""; }// www. j av a2s . c om String tmp = str; tmp = replace(tmp, "&", "&", false); tmp = replace(tmp, "<", "<", false); tmp = replace(tmp, "\"", """, false); tmp = replace(tmp, ">", ">", false); tmp = replace(tmp, " ", " ", false); tmp = replace(tmp, "\n", "<br>", false); /* * if (tmp.indexOf("\n") >= 0 ){ tmp = replace(tmp, "\n", " <br> ", * false); tmp = replace(tmp, " ", " ", false); } */ return tmp; } /** * replace b in a to c, concern uppercase?d=true:d=false */ public static String replace(String source, String findWhat, String replaceWith, boolean caseSence) { return replace(new StringBuffer(source), findWhat, replaceWith, caseSence).toString(); } public static String replace(String source, String findWhat, String replaceWith) { return replace(new StringBuffer(source), findWhat, replaceWith, true).toString(); // return source.replaceAll(findWhat, replaceWith); } public static StringBuffer replace(StringBuffer source, String findWhat, String replaceWith) { return replace(source, findWhat, replaceWith, true); } public static StringBuffer replace(StringBuffer source, String findWhat, String replaceWith, boolean caseSence) { if (source == null || findWhat == null || replaceWith == null) { return source; } if (findWhat.length() == 0) { return source; } int fromIndex = 0; int occIndex = -1; if (caseSence) { occIndex = source.toString().indexOf(findWhat, fromIndex); } else { occIndex = source.toString().toUpperCase().indexOf(findWhat.toUpperCase(), fromIndex); } while (occIndex >= 0) { source.delete(occIndex, occIndex + findWhat.length()); source.insert(occIndex, replaceWith); fromIndex = occIndex + replaceWith.length(); if (caseSence) { occIndex = source.toString().indexOf(findWhat, fromIndex); } else { occIndex = source.toString().toUpperCase().indexOf(findWhat.toUpperCase(), fromIndex); } } return source; } /** * Find the index of the searchStr in the string array strs. * Return -1, if not found. * * <pre> * StringUtil.indexOf(["s1", "s2"], "s1", true) = 0 * StringUtil.indexOf(["s1", "s2"], "S1", true) = -1 * </pre> * * @param strs * the string array to check, maybe null. * @param searchStr * the string to search for, maybe null. * @param caseSensive * is it case sensive while finding the searchStr * in the searchStr. * @return index of the searchStr found in the strs, -1 if not found. * * @author chenke */ public static int indexOf(String[] strs, String searchStr, boolean caseSensive) { if (strs == null || strs.length == 0) { return -1; } for (int i = 0; i < strs.length; i++) { if (isEqual(strs[i], searchStr, caseSensive)) { return i; } } return -1; } public static int indexOf(String[] strs, String searchStr) { return indexOf(strs, searchStr, true); } private static boolean isEqual(String s1, String s2, boolean caseSensive) { if (caseSensive) { return s1 == null ? s2 == null : s1.equals(s2); } else { return s1 == null ? s2 == null : s1.equalsIgnoreCase(s2); } } }