码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-03-16 14:22:40      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

转载自:转译特殊符号标签

 

/**
 * 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>"));
    }
}

 

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

标签:

原文地址:http://www.cnblogs.com/xubuhang/p/4341672.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!