2017-07-30 111 views
-3

如何從字符串中刪除單詞?例如,如果我有以下輸入:如何從Java中的字符串中刪除單詞?

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; 
} 
+2

使用String.replaceAll(「word」,「」)'方法有什麼問題。 –

+1

使用一些正則表達式('\\ s'來匹配空格) – 2017-07-30 17:11:01

+0

@OmarAhmed它爲上面的最後4個案例提供了冗餘空間 – hhj8i

回答

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; 

注:這種算法不一樣,如果字工作是連續的重複,我肯定不是語法:)。

+0

這不適用於前4種情況。儘管周圍有很多空格(如果有的話),但應該始終刪除該單詞。 – hhj8i

+0

修改後部分工作。但是,如果它出現兩次,它不會消除「致命的」。如果「致命」出現在結尾,它會重複字符串! – hhj8i

+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 +」將檢查多個常規空格。如果找到,它將被替換爲單個空間。

+0

它很直觀,但我想節省輸入空間,所以不是解決方案 – hhj8i

+0

通過節省輸入空間,您的意思是什麼?如果你不想修改輸入,那麼你可以創建一個新的數組,並在新的數組中,你可以把新的值。 – karna

+0

通過節省空間,我的意思是在「癌症是致命的疾病」中,我希望它是「癌症是一種疾病」而不是「癌症是一種疾病」。如果它們存在於字符串中,我不想刪除多個連續的空格。 – hhj8i

0

替換單詞「致命」與單個空格,

儘量拆分整個字符串用一個空格(」「),然後,同時進行迭代返回的字符串數組中刪除單個空間元素和合並回後,將帶有一個空格的單詞。所以這個不需要的空間將被刪除。

+0

空間不是不需要的 – hhj8i