Java 如何从另一个字符串中删除该字符串?
我想从另一个字符串中删除一个字符串,而不是所有的字母 示例:“你好,世界,我叫约翰”Java 如何从另一个字符串中删除该字符串?,java,string,Java,String,我想从另一个字符串中删除一个字符串,而不是所有的字母 示例:“你好,世界,我叫约翰” 删除:“ewo” 结果:“我叫约翰” 我的程序删除所有要删除的字母 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("");
删除:“ewo”
结果:“我叫约翰” 我的程序删除所有要删除的字母
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);
}
String text=“你好,世界,我叫约翰”;
int num=1;
for(int i=0;i
我的程序返回:“hll rld我的nam是jhn”,但这不是我需要的这可能是您想要的
System.out.prinln("hello world my name is john".replace("orld",""));
public static void main(String[] args) {
String str1 = "hello world my name is john";
String str2 = "ewo";
int currentCharIndex = 0;
StringBuilder resultBuilder = new StringBuilder();
for (char c : str1.toCharArray()) {
if (currentCharIndex >= str2.length() || c != str2.charAt(currentCharIndex)) {
resultBuilder.append(c);
} else {
currentCharIndex++;
}
}
System.out.println(resultBuilder.toString());
}
这可能是您想要的
public static void main(String[] args) {
String str1 = "hello world my name is john";
String str2 = "ewo";
int currentCharIndex = 0;
StringBuilder resultBuilder = new StringBuilder();
for (char c : str1.toCharArray()) {
if (currentCharIndex >= str2.length() || c != str2.charAt(currentCharIndex)) {
resultBuilder.append(c);
} else {
currentCharIndex++;
}
}
System.out.println(resultBuilder.toString());
}
从首选输出中,我认为您只希望替换第一个匹配字符。幸运的是,Java有一种方法可以实现这一点 替换此行:
text=text.replace(delArray[j],“”)代码>
关于这一点:
text=text.replaceFirst(delArray[j],“”)代码>
现在它只根据需要删除第一个匹配字符。从首选输出中,我认为您只想替换第一个匹配字符。幸运的是,Java有一种方法可以实现这一点
替换此行:
text=text.replace(delArray[j],“”)代码>
关于这一点:
text=text.replaceFirst(delArray[j],“”)代码>
现在,它只根据需要删除第一个匹配字符。您可以使用replaceFirst()
而不是replace()
。它将删除与输入匹配的第一个出现字符。您可以使用replaceFirst()
而不是replace()
。它将删除与输入匹配的第一次出现。您可以使用replaceFirst()或使用三个循环分别删除e、w和o,然后使用break语句。您可以使用replaceFirst()或使用三个循环分别删除e、w和o,然后使用break语句。尝试此操作
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
}
String text=“你好,世界,我叫约翰”;
int num=1;
for(int i=0;ihll-orld我的名字是约翰
}
试试这个
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
}
String text=“你好,世界,我叫约翰”;
int num=1;
for(int i=0;ihll-orld我的名字是约翰
}
但是。。。你需要什么?字符串“ewo”在“hello world我的名字是john”中的任何地方都没有出现#39;t尝试String.valueOf(detail.toString.charAt(position))
获取每个位置的值并根据您的要求删除它们。可能我的问题是错误的,但“hEllo WOrld我的名字是john”-->我想删除EWO,字符串中的字母可能不在每个位置的旁边other@NikolayPavlov增加预期产出显然会帮助其他人。但是。。。你需要什么?字符串“ewo”在“hello world我的名字是john”中的任何地方都没有出现#39;t尝试String.valueOf(detail.toString.charAt(position))
获取每个位置的值并根据您的要求删除它们。可能我的问题是错误的,但“hEllo WOrld我的名字是john”-->我想删除EWO,字符串中的字母可能不在每个位置的旁边other@NikolayPavlov添加预期的输出显然会帮助其他人。此代码甚至不会编译,而且它也不会达到OP的目的。替换orld
将以文字子字符串orld
为目标。但是OP想要删除orld
中的每个字符,一次删除一个。@TimBiegeleisen在问题中不清楚它应该做什么。所需的输出违反了您提到的规则,因为它导致hllo
-o
应该被删除,对吗?此代码甚至不会编译,也不会执行OP想要的操作。替换orld
将以文字子字符串orld
为目标。但是OP想要删除orld
中的每个字符,一次删除一个。@TimBiegeleisen在问题中不清楚它应该做什么。所需的输出违反了您提到的规则,因为它导致hllo
-o
应该被删除,对吗?