如何删除HTML特殊字符并将特殊字符替换为文本中的相应值?

时间:2012-07-27 05:32:22

标签: java

我需要删除HTML特殊字符,并将特殊字符替换为字符串中的相应值。

例如:

我有一个这样的字符串:

String text="Federation of AP Chambers of Commerce & Industry Awards for the year 2010-11. Speaking on the occasion, 
He said, "About 54 percent of the population is youth aged below 25 years. We have to use their energy and 
intelligence for development of the state as well as the country.The youth trained will also be absorbed by 
companies.’"

"需要替换为",而&需要替换为&,而’需要替换为

2 个答案:

答案 0 :(得分:4)

您无法使用API​​中的任何特定方法来执行此操作。使用以下方法。

String text="Federation of AP Chambers of Commerce & Industry Awards for the year 2010-11. Speaking on the occasion, 
He said, "About 54 percent of the population is youth aged below 25 years. We have to use their energy and 
intelligence for development of the state as well as the country.The youth trained will also be absorbed by 
companies.’&quot";

    text= replaceAll(text,""","\"");

    text= replaceAll(text,"&","&");

    text= replaceAll(text,"’","’");




private String replaceAll(String source, String pattern, String replacement) {
        if (source == null) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        int index;
        int patIndex = 0;
        while ((index = source.indexOf(pattern, patIndex)) != -1) {
            sb.append(source.substring(patIndex, index));
            sb.append(replacement);
            patIndex = index + pattern.length();
        }
        sb.append(source.substring(patIndex));
        return sb.toString();
    }

答案 1 :(得分:3)

看起来Jakarta Commons Lang图书馆的StringEscapeUtils.unescapeHtml() method会做你想要的。