2017-07-30 8 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) 

そして残りのために:私は、最初の4例のためではなく、最後の4出力を希望している

Cancer is a   disease 

Cancer is  a disease 

Cancer is a  disease 

Cancer is  a disease 

上記。最後の4例では、単語を削除した後に余分なスペースが残っています。

String deleteWord(String sentence,String... words) 
{ 
    for(String s:words) 
    { 
     sentence=sentence.replaceAll(s," "); 
    } 
    return sentence; 
} 
+2

'String.replaceAll(" word "、" ")'メソッドを使用するとどうなりますか? –

+1

いくつかの正規表現(スペースにマッチする '\\ s')を使用します。 –

+0

@OmarAhmedこれは最後の4つのケースに対して冗長なスペースを与えます – hhj8i

答えて

1

あなたの質問にマッチするように自分のコードを修正しました。最初は不連続なスペースに必要なものを誤解していました。このコードは単語の各トークンのフォーマットを保存します連続している)整数リストArrayListで、そのデータに基づいて文章が変更されています。今度は、繰り返し、開始時または終了時に機能します。言及した2種類の文が1つの文に含まれています。私ものArrayListをインポートすることを忘れないでくださいのStringBuilderを使用していたインデックスに応じて文字列を置換する

もちろん
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); 

} 


} 

: -

import java.util.ArrayList; 

注:このアルゴリズムは動作しない単語の場合逐語的に繰り返されますが、確かにそれは文法ではありません:)。

+0

これは最初の4つのケースでは機能しません。周囲のスペース(存在する場合)の数にかかわらず、単語は常に削除する必要があります。 – hhj8i

+0

変更後、部分的に機能します。しかし、それが2回現れても、 "致命的"は取り除かれません。最後に "致命的"がある場合は文字列を複製します! – hhj8i

+0

コードを変更してチェックアウトしました。 –

4

致命的な単語をスペースで置き換える必要があります。その後、各センテンス内の複数の通常のスペースをチェックします。通常のスペースが複数ある場合は、スペースを1つ置き換えてください。

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

単一スペースで「致命的な」という言葉を交換した後、

は、単一のスペースで文字列全体を分割してみてください(」「)、次いで、返される文字列の配列を反復しながら、単一のスペース要素を削除してバックマージその中に1つのスペースがある単語。したがって、この不要なスペースは削除されます。

+0

スペースは不要です – hhj8i

関連する問題