关于把<转化成&lt;的问题帮我看一下代码!!帮帮

zjgj 2002-06-04 10:10:41
下面这代是我写的,初学写的不对,帮我看看应该怎么写?
String age=myrs.getString("age");
age=age.replace(/</g,"<");
age=age.replace(/>/g,">");
String mysay=myrs.getString("mysay");
mysay=mysay.replace(/</g,"<");
mysay=mysay.replace(/>/g,">");
...全文
6 点赞 收藏 9
写回复
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjgj 2002-06-06
不好意思,我真是看不懂WalkSing(我行) 的代码
zijianyi(紫剑伊) 我按你的写了一下,但编译时出现错误
package str;
import java.text.*;
import java.util.*;
import java.lang.*;
public class str
{
public String str(String s)
{
String re;
re=replace(s,"<","<");
re=replace(re,">",">");
return re;
}
}
这是怎么回事呢?
回复
zjgj 2002-06-05
那你能不能写个例子让我看看,谢谢了!!
回复
hardas 2002-06-05
String.replace是字符代替字符不是字符串。仔细看文档。
回复
hcgui 2002-06-05
收藏!
回复
WalkSing 2002-06-05
package walksing.string;
import java.text.*;
import java.util.*;
public class wsString
{

/***$id added by WalkSing [wcsz@mail.china.com] 2003-03-22 9:57 ***/
public String Jreplace(String S,String T,String I)
{ /*$id:walksing walksing@163.com 2002-03-12 9:57 make.
*para S --Source String will find to be replaced String,which

will be overwrite with T;
*para T --Target String which will overwrite S;
*para I --Input String from which to find;
*return String replacement Result;
*note:S,T,I can be String Type;
*ver 1.0;
*/
if (I.length()<=0 || S.length()<=0 ) return I;
int ic=0;
String b1="";
char ch;
StringBuffer b = new StringBuffer(I.length()+6);
for (int i=0;i<I.length();i++)
{
ch=I.charAt(i);

if (ch==S.charAt(ic))//find one char of S
{
b1+=ch;//store to b1;
++ic;
if (ic==S.length())//find one String S from

String I
{
b1="";
ic=0;
b.append(T);//replace this S with T
}

}
else
{
if (ic==0) //not find
{
b.append(ch);

}
else if(ic!=0)
{// not equal all String S
b1+=ch;
b.append(b1.toString());//store b1;
ic=0; //set b.length=0
b1="";//set b1= null and to store

another
}
}
}
return b.toString();
}

public String[] split(String pattern ,String string)
{
/*@$ID 2002-03-25 9:38 China
*@para String pattern ,by rule to split the string
*@para String string ,which to be split from string
*example:
String str="server/$/user/$/password";
String[] str2=bbs.split("/$/",str);
then str2[0]="server",str2[1]="user",str2[2]="password";
*AUTHOR:WalkSing [chenshuzeng@163.net]
*VER:1.0
*/
String[] result= {};
char ch;
int ic=0;
int arrCount=0;
String element="";
String strBak="";
StringBuffer usage=new StringBuffer();
usage.append("/*$ID 2002-03-25 9:38 China\n");
usage.append("*@para String pattern ,by rule to split the

string\n");
usage.append("*@para String string ,which to be split from

string\n");
usage.append("*example:\n");
usage.append("String str=\"server/$/user/$/password\n\";");
usage.append("String[] str2=bbs.split(\"/$/\",str);\n");
usage.append("then

str2[0]=\"server\",str2[1]=\"user\",str2[2]=\"password\";\n");
usage.append("*AUTHOR:WalkSing [WalkSing@cnuninet.com

chenshuzeng@163.net]\n");
usage.append("*VER:1.0\n");
if (string.equals(""))
{
System.out.println(usage.toString());
result[0]= usage.toString();
return result;
}
for (int i=0;i<string.length();i++)//FIND ARRCOUNT
{
ch=string.charAt(i);
if (ch==pattern.charAt(ic))
{
ic++;//find next pattern ch
//strBak+=ch;
if (ic==pattern.length())//find one arrary

element
{
arrCount++;
ic=0; //resset ic
//strBak=""; //reset strBak
}
}
}
result=new String[arrCount+1];//STORE TO STRING[] ARRAY
ic=0;
arrCount=0;
for (int i=0;i<string.length();i++)//FIND ARRCOUNT
{
ch=string.charAt(i);
if (ch==pattern.charAt(ic))
{
ic++;//find next pattern ch
strBak+=ch;
if (ic==pattern.length())//find one arrary

element
{
result[arrCount]=element;//store

element to array
arrCount++;
ic=0; //resset ic
strBak=""; //reset strBak
element="";//reset element to store

next
}
continue;
}
if (ic!=0)
{
element+=strBak;
ic=0;
strBak="";
}
element +=ch;
}
if ((!element.equals(""))||(!strBak.equals("")))
{
element+=strBak;
result[arrCount]=element.toString();
strBak =null;
element=null;
}
if (result[arrCount]==(null))
result[arrCount]="";
return result;
}

public String getStr(String str)
{
try
{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp =new String(temp_t);
return temp;
}
catch(Exception e)
{
return null;
}
}
public String getHtmlFormat(String exp1)
{

String exp2="",dealinput="";
if (exp1==null||exp1.equals(""))
return dealinput;


exp2=Jreplace("<","<",exp1);


exp2=Jreplace(">",">",exp2);
exp2=Jreplace(" "," ",exp2);
exp2=Jreplace("'","''",exp2);
exp2=Jreplace("\n","<br>",exp2);
exp2=Jreplace("<br>","<br>",exp2);
exp2=AutoWrap(exp2, exp2.length());


dealinput=exp2;


return dealinput;


}
public String dealinput(String exp1)
{


String exp2="",dealinput="";
if (exp1==null||exp1.equals(""))
return dealinput;


exp2=Jreplace("<","<",exp1);


exp2=Jreplace(">",">",exp2);
exp2=Jreplace(" "," ",exp2);
exp2=Jreplace("'","''",exp2);
exp2=Jreplace("\n","<br>",exp2);
exp2=Jreplace("<br>","<br>",exp2);
exp2=AutoWrap(exp2, exp2.length());


dealinput=exp2;


return dealinput;


}
}//end class
///********************************************************************

*****//
回复
WalkSing 2002-06-05
package walksing.string;
import java.text.*;
import java.util.*;
public class wsString
{

/***$id added by WalkSing [wcsz@mail.china.com] 2003-03-22 9:57 ***/
public String Jreplace(String S,String T,String I)
{ /*$id:walksing walksing@163.com 2002-03-12 9:57 make.
*para S --Source String will find to be replaced String,which

will be overwrite with T;
*para T --Target String which will overwrite S;
*para I --Input String from which to find;
*return String replacement Result;
*note:S,T,I can be String Type;
*ver 1.0;
*/
if (I.length()<=0 || S.length()<=0 ) return I;
int ic=0;
String b1="";
char ch;
StringBuffer b = new StringBuffer(I.length()+6);
for (int i=0;i<I.length();i++)
{
ch=I.charAt(i);

if (ch==S.charAt(ic))//find one char of S
{
b1+=ch;//store to b1;
++ic;
if (ic==S.length())//find one String S from

String I
{
b1="";
ic=0;
b.append(T);//replace this S with T
}

}
else
{
if (ic==0) //not find
{
b.append(ch);

}
else if(ic!=0)
{// not equal all String S
b1+=ch;
b.append(b1.toString());//store b1;
ic=0; //set b.length=0
b1="";//set b1= null and to store

another
}
}
}
return b.toString();
}

public String[] split(String pattern ,String string)
{
/*@$ID 2002-03-25 9:38 China
*@para String pattern ,by rule to split the string
*@para String string ,which to be split from string
*example:
String str="server/$/user/$/password";
String[] str2=bbs.split("/$/",str);
then str2[0]="server",str2[1]="user",str2[2]="password";
*AUTHOR:WalkSing [chenshuzeng@163.net]
*VER:1.0
*/
String[] result= {};
char ch;
int ic=0;
int arrCount=0;
String element="";
String strBak="";
StringBuffer usage=new StringBuffer();
usage.append("/*$ID 2002-03-25 9:38 China\n");
usage.append("*@para String pattern ,by rule to split the

string\n");
usage.append("*@para String string ,which to be split from

string\n");
usage.append("*example:\n");
usage.append("String str=\"server/$/user/$/password\n\";");
usage.append("String[] str2=bbs.split(\"/$/\",str);\n");
usage.append("then

str2[0]=\"server\",str2[1]=\"user\",str2[2]=\"password\";\n");
usage.append("*AUTHOR:WalkSing [WalkSing@cnuninet.com

chenshuzeng@163.net]\n");
usage.append("*VER:1.0\n");
if (string.equals(""))
{
System.out.println(usage.toString());
result[0]= usage.toString();
return result;
}
for (int i=0;i<string.length();i++)//FIND ARRCOUNT
{
ch=string.charAt(i);
if (ch==pattern.charAt(ic))
{
ic++;//find next pattern ch
//strBak+=ch;
if (ic==pattern.length())//find one arrary

element
{
arrCount++;
ic=0; //resset ic
//strBak=""; //reset strBak
}
}
}
result=new String[arrCount+1];//STORE TO STRING[] ARRAY
ic=0;
arrCount=0;
for (int i=0;i<string.length();i++)//FIND ARRCOUNT
{
ch=string.charAt(i);
if (ch==pattern.charAt(ic))
{
ic++;//find next pattern ch
strBak+=ch;
if (ic==pattern.length())//find one arrary

element
{
result[arrCount]=element;//store

element to array
arrCount++;
ic=0; //resset ic
strBak=""; //reset strBak
element="";//reset element to store

next
}
continue;
}
if (ic!=0)
{
element+=strBak;
ic=0;
strBak="";
}
element +=ch;
}
if ((!element.equals(""))||(!strBak.equals("")))
{
element+=strBak;
result[arrCount]=element.toString();
strBak =null;
element=null;
}
if (result[arrCount]==(null))
result[arrCount]="";
return result;
}

public String getStr(String str)
{
try
{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp =new String(temp_t);
return temp;
}
catch(Exception e)
{
return null;
}
}
public String getHtmlFormat(String exp1)
{

String exp2="",dealinput="";
if (exp1==null||exp1.equals(""))
return dealinput;


exp2=Jreplace("<","<",exp1);


exp2=Jreplace(">",">",exp2);
exp2=Jreplace(" "," ",exp2);
exp2=Jreplace("'","''",exp2);
exp2=Jreplace("\n","<br>",exp2);
exp2=Jreplace("<br>","<br>",exp2);
exp2=AutoWrap(exp2, exp2.length());


dealinput=exp2;


return dealinput;


}
public String dealinput(String exp1)
{


String exp2="",dealinput="";
if (exp1==null||exp1.equals(""))
return dealinput;


exp2=Jreplace("<","<",exp1);


exp2=Jreplace(">",">",exp2);
exp2=Jreplace(" "," ",exp2);
exp2=Jreplace("'","''",exp2);
exp2=Jreplace("\n","<br>",exp2);
exp2=Jreplace("<br>","<br>",exp2);
exp2=AutoWrap(exp2, exp2.length());


dealinput=exp2;


return dealinput;


}
}//end class
///********************************************************************

*****//
回复
zijianyi 2002-06-05
//禁用HTML代码编码
public String html(String s){
String re;
re=replace(s,"<","<");
re=replace(re,">",">");
re=replace(re,"\n","<br>");
re=replace(re," "," ");
re=replace(re,"'","'");
return re;
}
回复
daanhan 2002-06-05
用replaceAll()可以替换字符串
不过要jdk1.4哦
回复
zjgj 2002-06-04
为什么没人帮忙啊??
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告