java - Java replaceSubstring() 方法涉及StringBuilder?
问题描述
我的任务的一部分是创建一个方法,用字符串 3 替换字符串 1 中出现的字符串 2。所以如果句子是:“狗跳过栅栏”,我希望方法替换任何字符串 2 的所有出现都可以说是“the”,而字符串 3 的内容可以说是“that”。
所以我想让它说“那只狗跳过了那个栅栏”。
如果我的老师教授允许更方便的方法,这真的很容易,但是整个课程在学习方面很不方便,所以我必须使用 StringBuilder 对象。
到目前为止,我的 replaceSubstring() 代码是
public static String replaceSubstring(String str1, String str2, String str3)
{
String str1Copy = str1, str2Copy = str2, str3Copy = str3;
if (str2Copy.equals(str3Copy))
{
return str1Copy;
}
StringBuilder b = new StringBuilder(str1Copy);
int index = b.indexOf(str2Copy);
b.replace(index, (index + str2Copy.length()), str3Copy);
index = b.indexOf(str3Copy);
return b.toString();
}
但是我遇到了一个问题,因为当我在打印出此方法的返回语句的应用程序类中运行此代码时,我得到
After replacing "the" with "that", the string: that dog jumped over the fence
在我的控制台中。原始字符串是“the dog jumped over the fence”,我的代码应该将其更改为“that dog jumped over that fence”,但它只是更改了“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();
}
解决方案
您需要在不再出现str2
in时循环str1
。indexOf()
如果不再出现,则返回 -1,因此您可以使用它来解决此问题。在循环内部使用重载indexOf(String str, int fromIndex)
。您也不需要String
's 的副本:
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()
没有指定起始索引的情况下调用,所以它将从 的开头开始String
:
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.
如果我们用 替换,而不指定起始索引,看看为什么这很重要the
,tthe
它看起来像这样:
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.
等等等等。
推荐阅读
- jpa - JPA生成策略表和序列的区别
- c++ - 创建子类的多个实例,只有一个超类的实例
- flutter - 如何对按钮进行编程以在点击和保持时连续增加或减少计数器?
- flutter - Flutter ListView itembuilder 不会显示任何内容。为什么?
- swiftui - iOS SwiftUI 在与代码交互时如何调出“嵌入 VStack”之类的额外操作?
- maven - 防止部署 Maven 模块
- python - 在 tensorflow 和 keras 上运行我的 Rnn 模型时出现 InvalidArgumentError
- angular - md-icon' 在使用 Angular Material 8.2.3 时不是已知元素错误,即使已导入 --
- javascript - document.getElementById("").focus() 不关注所需的项目。使用 setTimeout 的某些超时值有时会起作用
- java - 电子邮件未通过按钮单击 android 上的 gmail smtp 服务器发送