日期:2014-05-20  浏览次数:20747 次

html超链接如何转化为字符串的问题
假设现在我想分析一个锚标签<a href="http://tech.sina.com.cn/n/2008-05-22/08132210131.shtml" target="_blank">低预算购本攻略</a> 
但当我尝试将其转化为字符串时会出现错误Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ ),我已经用\恢复它字面意思的啊,我的字符串如下:String str = "<a href=\"http:\/\/tech\.sina\.com.cn\/n\/2008-05-22\/08132210131\.shtml\" target=\"_blank\">低预算购本攻略<\/a>";

------解决方案--------------------
String s = "<a href=\"http://tech.sina.com.cn/n/2008-05-22/08132210131.shtml\" target=\"_blank\">";
------解决方案--------------------
String str = "<a href='http://tech.sina.com.cn/n/2008-05-22/08132210131.shtml' target='_blank'>";
------解决方案--------------------
你可能需要这段代码
Java code
private String htmlFilter( String line ) {
        if( line == null || line.equals("") ) {
            return "";
        }
        // replace ampersands with HTML escape sequence for ampersand;
        line = replace(line, "&", "&#38;");
        // replace \" sequences with HTML escape sequences;
        line = replace(line, "\\\"", "&#92;&#34");
        // replace the \\ with HTML escape sequences. fixes a problem when
        // backslashes preceed quotes.
        line = replace(line, "\\\\", "&#92;&#92;" );
        // replace less-than signs which might be confused
        // by HTML as tag angle-brackets;
        line = replace(line, "<", "&#60;");
        // replace greater-than signs which might be confused
        // by HTML as tag angle-brackets;
        line = replace(line, ">", "&#62;");
        return multiLineCommentFilter(line);
    }

------解决方案--------------------
./在正则表达式中要转义,但在java字符串中是不需要转义的
String str = "\\\"\\.\\/";
写正则表达式要这样才行