涉及StringBuilder的Java replaceSubstring()方法?

涉及StringBuilder的Java replaceSubstring()方法?,java,string,substring,stringbuilder,Java,String,Substring,Stringbuilder,我任务的一部分是创建一个方法,用字符串3替换字符串1中出现的所有字符串2。因此,如果句子是:“狗跳过了栅栏”,我希望这个方法用字符串3的内容替换所有出现的字符串2,比如说是“the”,比如说是“that” 所以我想让它说“那只狗跳过了栅栏” 如果我的老师教授允许使用一种更方便的方法,这真的很容易,但整个课程在学习时都很不方便,所以我必须使用StringBuilder对象 到目前为止,我的replaceSubstring()代码是 然而,我遇到了一个问题,因为当我在打印出这个方法的返回语句的应用程

我任务的一部分是创建一个方法,用字符串3替换字符串1中出现的所有字符串2。因此,如果句子是:“狗跳过了栅栏”,我希望这个方法用字符串3的内容替换所有出现的字符串2,比如说是“the”,比如说是“that”

所以我想让它说“那只狗跳过了栅栏”

如果我的老师教授允许使用一种更方便的方法,这真的很容易,但整个课程在学习时都很不方便,所以我必须使用StringBuilder对象

到目前为止,我的replaceSubstring()代码是

然而,我遇到了一个问题,因为当我在打印出这个方法的返回语句的应用程序类中运行这段代码时,我得到了

After replacing "the" with "that", the string: that dog jumped over the fence
在我的控制台里。最初的字符串是“ThedogJumptheFense”,我的代码应该将其更改为“ThatdogJumptheFense”,但是它只是更改了第一个出现的“The”,而不是第二个。我真的很抓狂,因为我明白我怎么能做这样的事情

return string1.replaceAll(string2, string3);
今天到此为止,但如果我没有按照教授的要求使用StringBuilder对象,我会失去分数。我错过了什么?此外,我无法导入其他人创建的任何包。我必须使用通用和基本java工具包

编辑:似乎有效的新代码

   public static String replaceSubstring(String str1, String str2, String str3)
   {
      String str1Copy = new String (str1), str2Copy = new String (str2), str3Copy = new String (str3);

      if (str2Copy.equals(str3Copy))
      {
         return str1Copy;
      }

      StringBuilder b = new StringBuilder(str1Copy);

      int index = b.indexOf(str2Copy);
      while (index != -1)
      {
         b.replace(index, (index + str2Copy.length()), str3Copy);
         index = b.indexOf(str2Copy, index + 1);
      }

      return b.toString();
   }

您需要在
str1
中不再出现
str2
时循环
indexOf()
如果不再出现,则返回-1,以便您可以使用它来解决此问题。在循环内部使用重载。您也不需要
字符串的副本:

public static String replaceSubstring(String str1, String str2, String str3)
{ 
    if (str2.equals(str3))
    {
        return str1;
    }

    StringBuilder b = new StringBuilder(str1);
    int index = b.indexOf(str2); 
    //Loop while there is still an occurrence of str2       
    while(index != -1) {
        b.replace(index, (index + str2.length()), str3);
        index = b.indexOf(str2, index+str3.length());
    }

    return b.toString();
}

该行:

index = b.indexOf(str2, index+str3.length());
将搜索移动到已找到事件的位置。在第一次迭代中,我们只调用
indexOf()
,而不指定起始索引,因此它将从
字符串的开头开始:

the dog jumped over the fence
^index points to the first occurrence of the (0)
一旦我们调用
indexOf()
将起始索引指定为
index+str3.length()
,起始索引将是
0+4
,因此它会将搜索移动到:

that dog jumped over the fence
    ^Start the search here.
如果我们用
t替换
t
,而不指定起始索引,那么这一点很重要,原因如下:

the dog jumped over the fence
^found first occurrence. Replace with tthe
循环的第二次迭代:

tthe dog jumped over the fence
 ^Found another one! replace again.
ttthe dog jumped over the fence
  ^Another occurrence of the. 
循环的第三次迭代:

tthe dog jumped over the fence
 ^Found another one! replace again.
ttthe dog jumped over the fence
  ^Another occurrence of the. 

等等,等等。

当然,它只会更改第一次出现的内容,因为在完成之前,没有循环可以一直这样做。请再试一次,这次使用循环,如果要替换的字符串根本不在那里,请确保它不会失败(如果发生这种情况,您当前的代码将失效,因为您从未检查
indexOf
是否返回
-1
)。当我尝试此操作时,会出现一个错误,显示“线程中出现异常”“java.lang.NoSuchMethodError:StringOperations.replaceSubstring(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;在StringOperationsDemo.main(StringOperationsDemo.java:33)“代码现在似乎工作得很好。代码的底部写着index=b.indexOf(str2,index+str3.length());出于好奇,这到底是做什么的?