2016-10-17 10 views
0

文字列を置換せずに文字列内の単語を置き換える方法はありますか?文字列を使用せずに文字列を置換する

これはすべてハードコードされています。動的にする方法はありますか?私は動的にそれを作ることができるいくつかのライブラリファイルがあると聞いたが、私はあまりよく分からない。

私にいくつかの解決策を教えてもらえますか?どうもありがとうございました。この場合

for (int i = 0; i < results.size(); ++i) { 
       // To remove the unwanted words in the query 
       test = results.toString(); 
       String testresults = test.replace("numFound=2,start=0,docs=[",""); 
       testresults = testresults.replace("numFound=1,start=0,docs=[",""); 
       testresults = testresults.replace("{",""); 
       testresults = testresults.replace("SolrDocument",""); 
       testresults = testresults.replace("numFound=4,start=0,docs=[",""); 
       testresults = testresults.replace("SolrDocument{", ""); 
       testresults = testresults.replace("content=[", ""); 
       testresults = testresults.replace("id=", ""); 
       testresults = testresults.replace("]}]}", ""); 
       testresults = testresults.replace("]}", ""); 
       testresults = testresults.replace("}", ""); 
+1

[*正規表現(*)*](https://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/)を使用してください。 –

+0

result.toString()を実行しているだけでループするのはなぜですか?私は私が使用されて表示されません。ただ質問する... – alexbt

+0

基本的に私はSolrから情報/コンテンツを取得しているので。しかし、結果を表示すると、私が置き換えたそれらの不要な単語が表示されます。それは静的であり、動的にしたいのです。 @Alex –

答えて

0

、あなたはすべての可能な不要な単語をキャプチャするためにregular expressionと組み込みのString関数String.replaceAll()を学ぶ必要があるでしょう。たとえば :あなたの上記の例の方法では

public static String customReplace(String inputString, String replaceWith, String... stringsToReplace) { 
    if (inputString.equals("")) { return replaceWith; } 
    if (stringsToReplace.length == 0) { return inputString; } 

    for (int i = 0; i < stringsToReplace.length; i++) { 
     inputString = inputString.replace(stringsToReplace[i], replaceWith); 
    } 

    return inputString; 
} 

test.replaceAll("SolrDocument|id=|content=\\[", ""); 
0

は、単にその中にString.replace()メソッドを使用するために起こるカスタムString.replace()メソッドを作成して使用コンマ(、)で区切られていれば、stringsToReplaceのパラメータ内で置き換えたい文字列をいくつでも指定できます。それらはすべて、replaceWithパラメータに指定したものに置き換えられます。ここで

は、それがどのように使用できるかの例です:

String test = "This is a string which contains numFound=2,start=0,docs=[ crap and it may also " 
      + "have numFound=1,start=0,docs=[ junk in it along with open curly bracket { and " 
      + "the SolrDocument word which might also have ]}]} other crap in there too."; 

testResult = customReplace(strg, "", "numFound=2,start=0,docs=[ ", "numFound=1,start=0,docs=[ ", 
         + "{ ", "SolrDocument ", "]}]} "); 

System.out.println(testResult); 

あなたはまた、その要素内のすべてのあなたの不要な文字列が含まれており、stringsToReplaceパラメータにその配列を渡す、単一の文字列配列を渡すことができます

String test = "This is a string which contains numFound=2,start=0,docs=[ crap and it may also " 
      + "have numFound=1,start=0,docs=[ junk in it along with open curly bracket { and " 
      + "the SolrDocument word which might also have ]}]} other crap in there too."; 

String[] unwantedStrings = {"numFound=2,start=0,docs=[ ", "numFound=1,start=0,docs=[ ", 
          "{ ", "SolrDocument ", "]}]} "}; 

String testResult = customReplace(test, "", unwantedStrings); 

System.out.println(testResult); 
+0

ただの質問ですが、これはまだハードコードされているとカウントされていますか?申し訳ありませんが私の質問は、私はプログラミングに新しいときに愚かな音。 –

+0

ソースコード内に直接配置されている文字列アイテムではなく、置き換えたい文字列アイテムからなる変数を渡すため、ハードコーディングは考慮されません。 unwantedStrings変数は、テキストファイル内のコンテンツからでも、任意のソースによって動的に埋められます。 – DevilsHnd

+0

ハードコーディングとは、実際のデータ値(3や "abcd"のような実際の数字や文字列のように)をソースコードで使用する代わりに、入力値を受け入れる変数です。 – DevilsHnd

関連する問題