Posted on 2005-11-06 16:53
廖海瀛 阅读(522)
评论(0) 编辑 收藏
public class CodeFilter
{
public CodeFilter()
{
}
public static String toHtml(String s)
{
if(s == null)
{
s = "";
return s;
} else
{
s = Replace(s, "&", "&");
s = Replace(s, "<", "<");
s = Replace(s, ">", ">");
s = Replace(s, "\t", " ");
s = Replace(s, "\r\n", "\n");
s = Replace(s, "\n", "<br>");
s = Replace(s, " ", " ");
s = Replace(s, "'", "'");
s = Replace(s, "\\", "\");
return s;
}
}
public static String unHtml(String s)
{
s = Replace(s, "<img src='", "<img src='");
s = Replace(s, "'>", "'>");
s = Replace(s, "<center>", "<center>");
s = Replace(s, "</center>", "</center>");
return s;
}
public static String Replace(String s, String s1, String s2)
{
StringBuffer stringbuffer = new StringBuffer();
int i = s.length();
int j = s1.length();
int k;
int l;
for(k = 0; (l = s.indexOf(s1, k)) >= 0; k = l + j)
{
stringbuffer.append(s.substring(k, l));
stringbuffer.append(s2);
}
if(k < i)
stringbuffer.append(s.substring(k));
return stringbuffer.toString();
}
}