假設我有一個這樣的字串。
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 時完成的。
謝謝。
uj5u.com熱心網友回復:
基于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));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/482720.html
上一篇:二維陣列中的分段錯誤
下一篇:添加/洗掉節點后確定關節點
