将特殊字符转换成HTML格式

资讯 驱动中国 若水 1,957 次阅读 0 条评论
分享 Q

  将特殊字符转换成HTML格式
     //Class toHTML
    //CopyRight:Writed By Yun Leo(php4er@sohu.com)

    //Last modify time:2003-8-31
    //Method: public String toHTML(String old_string)

    class toHTML
    {
    public String toHTML(String sStr)

    {
    if (sStr == null||sStr.equals(""))

    {
    return sStr;
    }
    String sTmp = new String();
    int i = 0;
    while (i <= sStr.length()-1)

    {
    if (sStr.charAt(i) == '\n')

    {
    sTmp = sTmp.concat("<br>");
    }
    else if(sStr.charAt(i) == ' ')

    {
    sTmp = sTmp.concat(" ");
    }
    else if(sStr.charAt(i) == '<')

    {
    sTmp = sTmp.concat("<");
    }
    else if(sStr.charAt(i) == '>')

    {
    sTmp = sTmp.concat(">");
    }
    else if(sStr.charAt(i) == 34) //34为双引号
    {
    sTmp = sTmp.concat(""");
    }
    else if(sStr.charAt(i) == 39) //若为单引号则用两个单引号代替
    {
    sTmp = sTmp.concat("''");
    }
    else
    {
    sTmp = sTmp.concat(sStr.substring(i,i+1));
    }
    i++;
    }
    return sTmp;
    }
    }
    //-----------End-----------

版权声明:本文由驱动中国整理发布,转载需注明出处与原文链接。如有疑问请联系 editor@qudong.com
本文价值 成为第一个评分的人
登录后为本文打分
网友评论0 条评论
正在回复 的评论取消
评论加载中…
🔐

登录后参与互动

评论、点赞均可赚积分
连续签到、邀请好友也有奖励 🎁

电脑端: 微信扫码登录 微信内: 一键登录

微信扫一扫

打开微信「扫一扫」,分享本文到朋友圈或好友