我有一个字符串变量:
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 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/