/**
* This method takes a string which may contain HTML tags (ie, <b>,
* <table>, etc) and converts the '<'' and '>' characters to
* their HTML escape sequences.
*
* @param input the text to be converted.
* @return the input string with the characters '<' and '>' replaced
* with their HTML escape sequences.
*/
public static final String escapeHTMLTags( String input )
{
//Check if the string is null or zero length -- if so, return
//what was sent in.
if( input == null || input.length() == 0 )
{
return input;
}
//Use a StringBuffer in lieu of String concatenation -- it is
//much more efficient this way.
StringBuffer buf = new StringBuffer( input.length() );
char ch = ' ';
for( int i = 0; i < input.length(); i++ )
{
ch = input.charAt( i );
if( ch == '<' )
{
buf.append( "<" );
}
else if( ch == '>' )
{
buf.append( ">" );
}
else if( ch == '&' )
{
buf.append( "&" );
}
else if( ch == '"' )
{
buf.append( """ );
}
else
{
buf.append( ch );
}
}
return buf.toString();
}
html代码过滤
最新推荐文章于 2024-12-07 13:58:06 发布
这段Java代码定义了一个名为escapeHTMLTags的方法,用于将输入字符串中的HTML标签字符(如<、>、&、\)转换为对应的HTML转义序列。若输入为空或长度为0则直接返回,使用StringBuffer提高效率。

1587

被折叠的 条评论
为什么被折叠?



