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

标签 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

如何在不使用 replacereplaceAll 的情况下执行此操作?

最佳答案

这听起来像是一个家庭作业问题或类似的问题,所以我会给你一些提示。

(1)看Java StringTokenizer

(2) 思考如何在循环中使用它,其工作原理类似于(伪代码):

while there are substrings left to process
    get the next substring in s
    if that is "abc"
       append "the" to the result
    else
       append s to the result

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

关于java - 不使用replace或replaceAll替换字符串内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5706458/

相关文章:

java - 使用 PHP Web 服务问题将数据从 Android 应用程序插入到 PostgreSQL 数据库

java - 无法在我的 web 应用程序中显示我的 pdf(不在阻止模式下)

java - 带有状态代码的 Jersey Viewable

powershell - 在Powershell中用缩进和换行符替换字符

c++ - 替换字符串数组中所有出现的字母

regex - 如何在 Notepad++ 替换中分隔正则表达式组号?

java - 链式方法调用中的通用规范

java.util.concurrent.ThreadPoolExecutor 奇怪的逻辑

JavaScript,无法检测换行符

javascript - jQuery - 替换字符串中的所有括号