Java 不使用replace或replaceAll替换字符串内容

Java 不使用replace或replaceAll替换字符串内容,java,replace,Java,Replace,我有一个字符串变量: String str = " abc boy abc funny os abc abcifff abc abc boy abc"; 我想将“abc”替换为“the”,这将导致: the boy the funny os the abcifff the the boy the 如果不使用replace或replaceAll,我如何做到这一点?这听起来像是家庭作业问题或类似问题,因此我将给您一些提示 (1) 看看Java类 (2) 思考如何在循环中使用它,其工作方式如下(伪

我有一个字符串变量:

String str = " abc boy abc funny os abc abcifff abc abc boy abc";
我想将“abc”替换为“the”,这将导致:

the boy the funny os the abcifff the the boy the

如果不使用
replace
replaceAll
,我如何做到这一点?

这听起来像是家庭作业问题或类似问题,因此我将给您一些提示

(1) 看看Java类

(2) 思考如何在循环中使用它,其工作方式如下(伪代码):


不要忘记处理单词之间的空格。

规则需要创造性的解释!使用模式类

Pattern.compile("abc").matcher(yourString).replaceAll("the")

根据评论,禁止
replaceAll
是因为误解,因此请尝试以下方法:

System.out.println(" abc boy abc funny os abc abcifff abc abc boy abc".replaceAll(
            "( ?)abc( +|$)", "$1the$2"));
输出:

这个男孩是个滑稽的男孩


你为什么不想使用replaceAll?我怀疑这可能是家庭作业。因为如果我使用replaceAll方法,它会在任何地方发生变化(例如:“abc boy abc full os abc ciff abc boy abc”变为“theboy the full os the theiff the boy the”)。但我只需要在“abc”的任何地方,只有它必须改变。@shree到目前为止你试过什么?你遇到了什么问题?我们中的许多人(大多数?)希望看到您的一些工作,而不仅仅是为您编写代码。除非你想聘请一名顾问,否则你可能想在上面发表文章。现在,不要因为简单就投票决定结束这项工作,因为你曾经也是一名新手。
System.out.println(" abc boy abc funny os abc abcifff abc abc boy abc".replaceAll(
            "( ?)abc( +|$)", "$1the$2"));