2013-12-11 102 views
-1

在這裏,我們應該添加該邏輯無論字母,在這些數組argi的url中,有一個特定的字...字符串「字」 - 我們在搜索中引入的一個字。請幫助。下面是代碼:如何添加一些搜索邏輯

public class Search { 

    private String word; 
    private String str=""; 

      public String getWord() { 
      return word; 
     } 

     public void setWord(String word) { 
      this.word = word; 
     } 
     public String getStr() { 
      return str; 
     } 

     public void setStr(String str) { 
      this.str = str; 
     } 

     private final Pattern TITLE = Pattern.compile("\\<title\\>(.*)\\<\\/title\\>"); 

     public String search(String url, String someword) { 

      try { 
       InputStreamReader in = new InputStreamReader(new URL(url).openStream(),"UTF-8"); 
       StringBuilder input = new StringBuilder(); 
       int ch; 
       while ((ch = in.read()) != -1) { 
        input.append((char) ch); 
       } 
       if (Pattern.compile(someword).matcher(input).find()) { 
        Matcher title = TITLE.matcher(input); 
        if (title.find()) { 
         return title.group(1); 
        } 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (PatternSyntaxException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 
     public String toString() { 
      String[] argi = {"http://localhost:8080/site/endipnagradi", "http://localhost:8080/site/contacts_en", "http://localhost:8080/site/news_en"}; 

      for (int i = 0; i < argi.length; i++) { 
      String result = search(argi[i], word); 
      String regex = "^[А-Яа-я]+$"; 


      if (result != null && word.length()>2) { 

        str += "Search phrase " + "<b>"+ word + "</b>" + " have found in " + "<a href=\"" + argi[i] + "\">" + result + "</a>"+ "<p></p>"; 

        } 

      if(word.length()<3 || word.matches(regex)){ 

       str="Word not found!"; 
      } 

      if (word == null || word.isEmpty()) { 

       str = "Enter a search word!"; 

       } 
      } 
      return null; 
    } 
} 

例如,我把字「電話」,但我的內容只是字「電話」。所以無論如何搜索應該找到它

回答

0

用正確的選項創建你的模式,以便它會做一個不區分大小寫的匹配。 從更改代碼:

if (Pattern.compile(someword).matcher(input).find()) 

if (Pattern.compile(someword, Pattern.CASE_INSENSITIVE).matcher(input).find()) 

欲瞭解更多信息,看看here

+0

超級,它的工作!謝謝! –

1

你可以使用equalsIgnoreCase();。然後你可以有一個解決方案

"phone".equalsIgnoreCase("Phone") ; // this will return true and found the word 
+0

但我有可變的字符串單詞。如何將它添加到我的代碼? –