java中关于html的转换
2008-01-05 09:11:27 来源:WEB开发网核心提示:请看下面这个例子public static String escapeHtmlTags( String input ) {// Check if the string is null or zero length -- if so, return what was sent in.if( input == null
请看下面这个例子
public static 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();
char ch = ' ';
for( int i=0; i<input.length(); i++ ) {
ch = input.charAt(i);
if( ch == '<' ) {
buf.append( "<" );
}
else if( ch == '>' ) {
buf.append( ">" );
}
else {
buf.append( ch );
}
}
return buf.toString();
}
更多精彩
赞助商链接