[英]How to find words in a string from given list of keywords in java
假设我有一个这样的字符串。
String s = "I have this simple string java, I want to find keywords in this string";
同时,我有一个这样的关键字列表。
ArrayList<String> keywords = new ArrayList<>( Arrays.asList("string", "keywords"));
我想要实现的是从关键字中找出 s 中的单词并生成一个带有突出显示的关键字的新字符串。like
String sNew = "我有这个简单的字符串java,我想在这个字符串中查找关键字";
这个突出显示部分是通过 iText 在生成 pdf 时完成的。
谢谢。
基于https://stackoverflow.com/a/27081059/7212686 ,添加具有自定义字体的Chunk
public static final Font BLACK_NORMAL = new Font(FontFamily.HELVETICA, 12, Font.NORMAL);
public static final Font BLACK_BOLD = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
String s = "I have this simple string java, I want to find keywords in this string";
List<String> keywords = Arrays.asList("string", "keywords");
Paragraph p = new Paragraph();
for (String word : s.split("\\s+")) {
if (keywords.contains(word))
p.add(new Chunk(word, BLACK_BOLD));
else
p.add(new Chunk(word, BLACK_NORMAL));
p.add(new Chunk(" ", BLACK_NORMAL));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.