读书人

找寻一遍文章包涵关键字最短的字符串

发布时间: 2012-09-28 00:03:35 作者: rapoo

寻找一遍文章包涵关键字最短的字符串

?

package cn.edu.zzuli.object;import java.util.ArrayList;import java.util.List;/** * source="Provides Providesddd Providesthe Providesclasses Provids necessary to * create an applet and the classes an applet uses to communicate with its * applet context" *  * key=key[] = { "Pr", "vi", "o" } *  * 要求统计文章中包涵关键字的中最短的字符串 *  * @author moon *  */public class Test {public static void main(String[] args) {String source = "Provides Providesddd  Providesthe Providesclasses Provids necessary to create an applet and the classes an applet uses to communicate with its applet context";String key[] = { "Pr", "vi", "o" };String srcString = extractSummary(source, key);System.out.println(srcString);}public static String extractSummary(String description, String[] keyword) {String[] strTemp = description.split(" ");List<String> strcontainAll = new ArrayList<String>();boolean state = false;// 取得符合条件的字串数组for (String str : strTemp) {str = str.trim();for (String str2 : keyword) {if (!str.contains(str2)) {state = false;break;}state = true;}if (state) {strcontainAll.add(str);}}// 长度最短的子串String fhString = "";if (strcontainAll.size() > 0) {fhString = strcontainAll.get(0);for (String eachstr : strcontainAll) {if (fhString.length() > eachstr.length()) {fhString = eachstr;}}return fhString;} else {return "";}}}
?

读书人网 >编程

热点推荐