java - 如何从另一个字符串中删除字符串?
问题描述
我想从另一个字符串中删除一个字符串,而不是所有字母。
示例:“hello world,我的名字是 john”
删除:“ewo”
结果:“hllo rld 我的名字是 john”
我的程序删除了所有删除的字母
String text = "hello world my name is john";
int num = 1;
for (int i = 0; i < num; i++) {
String del = ewo;
String[] delArray = del.split("");
for (int j = 0; j < delArray.length; j++) {
text = text.replace(delArray[j], "");
}
System.out.println(text);
}
我的程序返回:“hll rld my nam is jhn”,但这不是我需要的
解决方案
尝试这个
String text = "hello world my name is john";
int num = 1;
for (int i = 0; i < num; i++) {
String del = ewo;
String[] delArray = del.split("");
for (int j = 0; j < delArray.length; j++) {
text = text.replaceFirst(delArray[j], "");
}
System.out.println(text); //output => hll orld my name is john
}
推荐阅读
- c# - 跟踪指标值应用程序洞察错误?
- loops - 时间复杂度和几何级数,嵌套循环
- angular - Angular 7:自定义异步验证器
- c++ - 如何正确地将 void * 值转换为 CString
- python - Python如何将这种类型的列表转换为字典
- php - 从给定的日期时间创建相同的时间差异
- scala - 在 Spark 中计算 Hive 表的统计信息
- kotlin - 如何用 kotlin 调用不同版本的类似扩展方法
- php - VsCode PHP-Intellisense 使用 composer 和 autoload.php
- spring-data-jpa - 如何将规范传递给 spring 数据的存储库接口上的自定义查询?