android 转译特殊符号标签(转载)

时间:2022-04-03 08:49:20
转载自:转译特殊符号标签

 

/**
 * DealingCharacter.java
 * Description: 
 * @author li.b
 * @version 2.0
 * Jun 27, 2008
 */
public class DealingCharacter {

    /**
     * Description: 转译特殊符号标签
     * @param value 需要处理的字符串
     * @return 
     */
     public static String filter(String value)
        {
            if(value == null || value.length() == 0)
                return value;
            StringBuffer result = null;
            String filtered = null;
            for(int i = 0; i < value.length(); i++)
            {
                filtered = null;
                switch(value.charAt(i))
                {
                case 60: // '<'
                    filtered = "&lt;";
                    break;

                case 62: // '>'
                    filtered = "&gt;";
                    break;

                case 38: // '&'
                    filtered = "&amp;";
                    break;

                case 34: // '"'
                    filtered = "&quot;";
                    break;

                case 39: // '/''
                    filtered = "&#39;";
                    break;
                }
                if(result == null)
                {
                    if(filtered != null)
                    {
                        result = new StringBuffer(value.length() + 50);
                        if(i > 0)
                            result.append(value.substring(0, i));
                        result.append(filtered);
                    }
                } else
                if(filtered == null)
                    result.append(value.charAt(i));
                else
                    result.append(filtered);
            }

            return result != null ? result.toString() : value;
        }

     public static void main(String[] args) {
         System.out.println(DealingCharacter.filter("<HTML>sdfasfas</HTML>"));
    }
}