如何从字符串中删除单词?例如,如果我有以下输入:
Cancer is a fatal disease
Cancer is afataldisease
Cancer is a fataldisease
Cancer is afataldisease
Cancer is a fatal disease
Cancer is afataldisease
Cancer is a fataldisease
Cancer is afataldisease
输出应该是:
Cancer is a disease(for first 4 cases)
剩下的:
Cancer is a disease
Cancer is a disease
Cancer is a disease
Cancer is a disease
我有前4个案例的输出,但不是上面的4个案例。在最后4个案例中,删除单词后留下了额外的空格。
String deleteWord(String sentence,String... words)
{
for(String s:words)
{
sentence=sentence.replaceAll(s," ");
}
return sentence;
}
答案 0 :(得分:4)
你应该用空格替换致命一词。之后检查每个句子中的多个常规空格。如果您有多个常规空格,请将其替换为单个空格。
public class ReplaceWord {
public static void main(String[] args) {
String[] str = {
"Cancer is a fatal disease",
"Cancer is afataldisease",
"Cancer is a fataldisease",
"Cancer is afataldisease",
"Cancer is a fatal disease",
"Cancer is afataldisease",
"Cancer is a fataldisease",
"Cancer is afataldisease"};
for(String string: str) {
string = string.replace("fatal", " ").replaceAll("\\s+", " ");
System.out.println(string);
}
}}
正则表达式" \\ s +" 将检查多个常规空格。如果找到,它将被替换为单个空格。
答案 1 :(得分:1)
我修改了我的代码以匹配你的查询。我最初误解了你对非连续空间的需求,现在这段代码保存了你单词的每个单一标记的格式(它的左外,右索引和一个整数来告诉天气空间是连续)在整数ArrayList中并根据该数据修改句子。所以现在它在开始或结束时重复工作。即使你提到的两种句子都在一个句子中。
String sentence= " fatal Cancer is a fatal diseasefatal";//one example
String word="fatal";
int spaces=0;
int left=-1;
int right=0;
int nonspacechar=0;//leftmost nonspacechar
int difference=0;//difference between the outermost indexes
int consecspaces=0;
ArrayList<Integer> format = new ArrayList();//declare arrayList
for(int i =0;i<sentence.length();i++){
if(i==sentence.indexOf(word,right)){//to get the left index after the word's right index
if(nonspacechar==0){
left=nonspacechar;
}else
left=nonspacechar+1;
format.add(left);//adding the left index to the arraylist
if(sentence.length()-i==word.length()){//if the word is at the end
right=i+word.length();
format.add(right);
format.add(2);
break;
}
i=i+word.length()-1;
continue;
}
if(sentence.charAt(i)!=' '){
if(left>=0){//to get the right non space index when left is set
right=i;
format.add(right);//add right outermost index
if(i==sentence.length()-1&&sentence.charAt(i)=='.'){
format.add(2);
}else
if(spaces>1){
format.add(1);
}else if(nonspacechar==0){
format.add(2);
}else
format.add(0);
left=-1;//reset left
}
nonspacechar=i;
spaces=0;
}else if(left>=0&&spaces==1){
spaces=0;
}else
spaces++;
}
int k=0;
StringBuilder newSentence=new StringBuilder(sentence);//declare the newsentence Stringbuilder to be replaced acording to indexes
while(k<format.size()){
left=format.get(k);
right=format.get(k+1);
if(k>0){
if(consecspaces==1){//check last consecuative space condition
left=left-difference;
right=right-difference;
}else{
left=left-(difference);//subtract last difference
right=right-(difference);
}
}
consecspaces=format.get(k+2);
if(consecspaces==1){//check if consecuativespaces exist
difference=difference+word.length();//adding word on difference
newSentence=newSentence.replace(newSentence.indexOf(word,left),newSentence.indexOf(word,left)+word.length(),"");
}else if(consecspaces==2){
difference=difference+(right-left);
newSentence=newSentence.replace(left,right,"");
}else{
difference=difference+(right-left)-1; //adding word on difference
newSentence=newSentence.replace(left,right," ");
}
k=k+3;
}
sentence=new String(newSentence);//get the new sentence
System.out.print(sentence);
}
}
当然要根据索引替换字符串我必须使用StringBuilder也不要忘记导入Arraylist: -
import java.util.ArrayList;
注意:如果这个词是连续重复的,那么这个算法不起作用,我肯定不是语法:)。
答案 2 :(得分:0)
更换单词&#34;致命&#34;单个空格,
尝试使用单个空格(&#34;&#34;)拆分整个字符串,然后在迭代返回的字符串数组时删除单个空格元素,并将单词空间合并回来。因此,这些不需要的空间将被删除。