请看下面这个例子
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
ch = input.charat(i);
if( ch == < ) {
buf.append( "
}
else if( ch == > ) {
buf.append( ">" );
}
else {
buf.append( ch );
}
}
return buf.tostring();
}