2017-04-05 130 views
-1

子字符串搜索這是我做過什麼:防止重複串JAVA

String[] conjunctions = new String[] { 
      "after", "although", "as if", 
      "as long as", "as much as", "as soon as", 
      "as though", "because", "before", "by the time", 
      "even if", "even though", "if", 

    }; 
    for (String toSearch : conjunctions) { 
     int occurrence = textToAnalyse.split("(?i)\\W" + toSearch +"\\W").length - 1; 
     System.out.println(toSearch + " X " + occurrence); 
    } 

例如,"if""even if"將被視爲是"if"同樣的事情也會被計算兩次。有沒有一種方法來優先搜索後者,並阻止JAVA搜索兩次?非常感謝

輸入:

textToAnalyse = "Even if you are smart, you are still dumb." 

預期輸出:

even if X 1 
if X 0 
+1

請正確解釋您的問題。添加輸入,輸出或預期輸出。 –

回答

1

你或許應該改變你的方法有點。考慮將連詞定義爲互斥正則表達式的列表,然後計算匹配的數量。作爲一個令人愉快的副作用,這種方法也將消除構建大量子串的需要,這應該有助於內存使用。