在這裏,我們應該添加該邏輯無論字母,在這些數組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;
}
}
例如,我把字「電話」,但我的內容只是字「電話」。所以無論如何搜索應該找到它
超級,它的工作!謝謝! –