用StringBuffer中的replace()方法。

解决方案 »

  1.   

    StringBuffer中的replace()可以用,不过得稍微麻烦一些,先要找到你所替换的subString的位置,然后再用replace。jdk1.4中的String.replaceAll()很方便,不过jdk1.4不知你又没有。
      

  2.   

    jdk1.4中的String.replaceAll()很方便,不过jdk1.4不知你又没有。没有的话到java.sun.com下载
      

  3.   

    我以前在学校里做记事本这个作业的时候,记得在TextArea里用了replaceRange()这个函数,非常好用,现在在网吧无法去查,你试试查查文档.
      

  4.   

    于字符串有关的方法都在java.lang.String,java.lang.StringBuffer这个2包内,好好看看会有帮助的
      

  5.   

    public String stringReplace(String sourceString, String toReplaceString, String replaceString)
      {
        String returnString = sourceString;
        int stringLength = 0;
        if(toReplaceString != null)
        {
          stringLength = toReplaceString.length();
        }
        if(returnString != null && returnString.length() > stringLength)
        {
          int max = 0;
          String S4 = "";
          for(int i = 0; i < sourceString.length(); i++)
          {
            max = i + toReplaceString.length() > sourceString.length()? sourceString.length():i + stringLength;
            String S3 = sourceString.substring(i, max);
            if(!S3.equals(toReplaceString))
            {
              S4 += S3.substring(0,1);
            }
            else
            {
              S4 += replaceString;
              i += stringLength -1 ;
            }
          }
          returnString = S4;
        }
        return returnString;
      }
      

  6.   

    给你个类,它以#1,#2为替换标志,方法都是static的:import java.io.*;
     public class StringReplacer{
        static String s =  "Congratulations, you have successfully booked air tickets with #1 through #2.";
    public StringReplacer(){
    }    /*
    * Specify the substring to be replaced and also the new substring to put into the original String.
    * @param s the original String
    * code1 substring in the original String to be replaced
    * replacement1 String to take place of code1
    * code2 another substring to be replaced
    * replacement2 String to take place to code2
    */
    public static String replace(String s, String code, String replacement){
    int startInt = 0, endInt = 0; startInt = s.indexOf(code); if (startInt == -1){return s;} endInt = startInt + code.length(); s = s.substring(0, startInt) + replacement + s.substring(endInt, s.length()); return s;
    }    /*
    * Specify the substring to be replaced and also the new substring to put into the original String.
    * @param s the original String
    * code1 substring in the original String to be replaced
    * replacement1 String to take place of code1
    * code2 another substring to be replaced
    * replacement2 String to take place to code2
    */
        public static String replace(String s, String code1, String replacement1, String code2, String replacement2){
      s = replace(s, code1, replacement1);
    s = replace(s, code2, replacement2);
            return s;
        }
    public static void main(String args[]){
    System.out.println(StringReplacer.replace(s, "#1", "Airline Company A", "#2", "Airline Agency B"));
            System.out.println(s);
        }
    }