java,php,asp.net,linux,javascript,mysql,mssql,oracle,编程

处理特殊符号的工具类

转载请注明:http://www.pmjava.com/Article/ShowInfo.asp?ID=56591
/**
* 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 = "<";
                   
break;

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

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

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

               
case 39: // '\''
                    filtered = "'";
                   
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(
"sdfasfas"));
    }
}

posted on 2009-06-10 21:29 rrong_m 阅读(622) 评论(0)  编辑  收藏

<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

导航

统计

常用链接

随笔档案

文章分类

文章档案

java编程

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜