如何在java中从字符串中删除子字符串

如何在java中从字符串中删除子字符串,java,string,Java,String,我需要预处理一个字符串并从中删除一些单词。我要找的是:- private static final String[] stopWords = { "is", "on", "of", "with"}; String s1 = "Tiger is Mammal with sharp teeth"; System.out.println("s1 = " + s1); // "Tiger is Mammal with sharp teeth" s1.remove(stopWords); //remo

我需要预处理一个字符串并从中删除一些单词。我要找的是:-

private static final String[] stopWords = { "is", "on", "of", "with"};
String s1 = "Tiger is Mammal with sharp teeth";
System.out.println("s1 = " + s1);  // "Tiger is Mammal with sharp teeth"
s1.remove(stopWords);  //remove should remove 'is' and 'with'  from s1
System.out.println("s1 = " + s1); // "Tiger Mammal sharp teeth"
<>我对编程很感兴趣,所以请考虑一下。

你可以这样做: 这将删除单词,但在两侧保留空格,因此在此之后,您可能希望将双空格替换为单空格:

s1 = s1.replace("  "," ");

首先有一个文档。所以我建议你看一下:当然,先生,我会看一下文件。
s1 = s1.replace("  "," ");