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

 

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 "";
		}
	}
}
 

你可能感兴趣的:(字符串)