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

此文章提供了一个方法来从给定的源文本中提取包含特定关键字的最短字符串。该过程涉及到文本分割、关键字匹配和筛选最短匹配项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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 "";
		}
	}
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值