public static String toHTMLString(String in) {
StringBuffer out = new StringBuffer();
for (int i = 0; in != null && i < in.length(); i++) {
char c = in.charAt(i);
if (c == '\'')
out.append("'");
else if (c == '\"')
out.append(""");
else if (c == '<')
out.append("<");
else if (c == '>')
out.append(">");
else if (c == '&')
out.append("&");
else if (c == ' ')
out.append(" ");
else if (c == '\n')
out.append("<br/>");
else
out.append(c);
}
return out.toString();
}
StringBuffer out = new StringBuffer();
for (int i = 0; in != null && i < in.length(); i++) {
char c = in.charAt(i);
if (c == '\'')
out.append("'");
else if (c == '\"')
out.append(""");
else if (c == '<')
out.append("<");
else if (c == '>')
out.append(">");
else if (c == '&')
out.append("&");
else if (c == ' ')
out.append(" ");
else if (c == '\n')
out.append("<br/>");
else
out.append(c);
}
return out.toString();
}
解决方案 »
- org.springframework.orm.hibernate3.LocalSessionFactoryBean的疑惑
- Jacob操作word表格问题
- FileOutputStream("file")关闭问题
- 有意者请进(人才需求)?
- 初学jsp,求问怎么开始学!
- 用 response 对象转到别的页面的方法是什么啊?是 response.redirect 吗???
- 远程桌面
- 请问:tomcat目前用什么版本的好v4.1.29?v5.0.14-beta?还是v5.0.16?
- jsp tag 在TLD中的描述是什么?
- 继承接口的问题~帮帮帮
- String ssql="INSERT INTO usercode VALUES ('"+name1+"','"+password+"','"+email+"','500','"+url+"','"+QQ+"')";有问题吗?在SQL Seve
- 关于session和request的问题?
private String strrequest(String input){
if(input!=null){
try { // public static String replaceHTML(String input)
char ch=' ';
StringBuffer buf=new StringBuffer(input.length()+6);
if(input==null || input.length()==0)return input;
for(int i=0 ;i<input.length(); i++){
ch=input.charAt(i);
if(ch==13) buf.append("");//»Ø܇æI
if(ch=='<')buf.append("<");
else if(ch=='>')buf.append(">");
else if(ch=='\'')buf.append("´");
else if(ch=='\"')buf.append(""");
//else if(ch==' ')buf.append(" ");
else
buf.append(ch);
}
input=buf.toString(); byte[] b_inputtemp=input.getBytes("8859_1");
String temp=new String(b_inputtemp,"big5");
return temp;
}
catch(Exception e){
return input;
}
}
else
return "";
}
然后再调用这个函数就可以了。你试试吧?
import java.lang.String;
import java.io.*;public class HtmlEncode {
String re;
public String replace(String con ,String tag,String rep){
int j=0;
int i=0;
int k=0;
String RETU="";
String temp =con;
int tagc =tag.length();
while(i<con.length()){
if(con.substring(i).startsWith(tag)){
temp =con.substring(j,i)+rep;
RETU+= temp;
i+=tagc;
j=i;
}
else{
i+=1;
}
}
RETU +=con.substring(j);
return RETU;
}public String HtmlEncode(String s){
re=replace(s,"<","<");
re=replace(re,">",">");
re=replace(re,"\n","<br>");
re=replace(re," "," ");
//re=replace(re,"'","'");
return re;
}
}