六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 115|回复: 0

HTMLEscape JavaScriptEscape

[复制链接]

升级  10.75%

389

主题

389

主题

389

主题

探花

Rank: 6Rank: 6

积分
1215
 楼主| 发表于 2013-2-7 19:18:31 | 显示全部楼层 |阅读模式
/**     * Returns a string that is equivalent to the input string, but with     * special characters converted to HTML escape sequences.     *     * @param input the string to escape (<code>null</code> not permitted).     *     * @return A string with characters escaped.     *     * @since 1.0.9     */    public static String htmlEscape(String input) {        if (input == null) {            throw new IllegalArgumentException("Null 'input' argument.");        }        StringBuffer result = new StringBuffer();        int length = input.length();        for (int i = 0; i < length; i++) {            char c = input.charAt(i);            if (c == '&') {                result.append("&");            }            else if (c == '\"') {                result.append(""");            }            else if (c == '<') {                result.append("<");            }            else if (c == '>') {                result.append(">");            }            else if (c == '\'') {                result.append("'");            }            else if (c == '\\') {                result.append("\");            }            else {                result.append(c);            }        }        return result.toString();    }    /**     * Returns a string that is equivalent to the input string, but with     * special characters converted to JavaScript escape sequences.     *     * @param input the string to escape (<code>null</code> not permitted).     *     * @return A string with characters escaped.     *     * @since 1.0.13     */    public static String javascriptEscape(String input) {        if (input == null) {            throw new IllegalArgumentException("Null 'input' argument.");        }        StringBuffer result = new StringBuffer();        int length = input.length();        for (int i = 0; i < length; i++) {            char c = input.charAt(i);            if (c == '\"') {                result.append("\\\"");            }            else if (c == '\'') {                result.append("\\'");            }            else if (c == '\\') {                result.append("\\\\");            }            else {                result.append(c);            }        }        return result.toString();    }} 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表