String.replace是字符代替字符不是字符串。仔细看文档。

解决方案 »

  1.   

    用replaceAll()可以替换字符串
    不过要jdk1.4哦
      

  2.   

    //禁用HTML代码编码
    public String html(String s){
    String re;
    re=replace(s,"<","&lt;");
        re=replace(re,">","&gt;");
        re=replace(re,"\n","<br>");
        re=replace(re," ","&nbsp;");
        re=replace(re,"'","&#39");
        return re;
    }
      

  3.   

    package walksing.string;
    import java.text.*;
    import java.util.*; 
    public class wsString
    { /***$id added by WalkSing [[email protected]] 2003-03-22 9:57 ***/
    public String Jreplace(String S,String T,String I)
     {  /*$id:walksing [email protected] 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 [[email protected]]
    *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 [[email protected] [email protected]]\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("<","&lt;",exp1);                                                                                                
      exp2=Jreplace(">","&gt;",exp2);    
      exp2=Jreplace(" ","&nbsp;",exp2);
      exp2=Jreplace("'","''",exp2);
      exp2=Jreplace("\n","<br>",exp2); 
      exp2=Jreplace("&lt;br&gt;","<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("<","&lt;",exp1);                                                                                                
      exp2=Jreplace(">","&gt;",exp2);    
      exp2=Jreplace(" ","&nbsp;",exp2);
      exp2=Jreplace("'","''",exp2);
      exp2=Jreplace("\n","<br>",exp2); 
      exp2=Jreplace("&lt;br&gt;","<br>",exp2);  
      exp2=AutoWrap(exp2, exp2.length());                                                       
      dealinput=exp2;                                                                                           
      return dealinput;                                                                                                

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

  4.   

    package walksing.string;
    import java.text.*;
    import java.util.*; 
    public class wsString
    { /***$id added by WalkSing [[email protected]] 2003-03-22 9:57 ***/
    public String Jreplace(String S,String T,String I)
     {  /*$id:walksing [email protected] 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 [[email protected]]
    *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 [[email protected] [email protected]]\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("<","&lt;",exp1);                                                                                                
      exp2=Jreplace(">","&gt;",exp2);    
      exp2=Jreplace(" ","&nbsp;",exp2);
      exp2=Jreplace("'","''",exp2);
      exp2=Jreplace("\n","<br>",exp2); 
      exp2=Jreplace("&lt;br&gt;","<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("<","&lt;",exp1);                                                                                                
      exp2=Jreplace(">","&gt;",exp2);    
      exp2=Jreplace(" ","&nbsp;",exp2);
      exp2=Jreplace("'","''",exp2);
      exp2=Jreplace("\n","<br>",exp2); 
      exp2=Jreplace("&lt;br&gt;","<br>",exp2);  
      exp2=AutoWrap(exp2, exp2.length());                                                       
      dealinput=exp2;                                                                                           
      return dealinput;                                                                                                

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

  5.   

    不好意思,我真是看不懂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,"<","&lt;");
        re=replace(re,">","&gt;");
        return re;
    }
    }
    这是怎么回事呢?