Fetch the repository succeeded.
This action will force synchronization from 陌溪/LearningNotes, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
前阵子一直在改造蘑菇博客的搜索模块,因为之前是使用的Solr搜索,每次搜索返回的时候,会自带高亮字段,后面改成SQL的方式实现搜索后,也想着能够增加高亮字段,因此就想着我们通过like关键字SQL查询出来后,能够在给关键字段添加一个标签
<span style = 'color:red'></span>
这样就能够让关键字能够出现想要的高亮效果了
但是这样存在一个问题就是,我们如何能够忽略大小写,给关键字添加高亮呢
其实我们的算法实现原理是比较简单的:
/**
* 添加高亮
* @param str
* @param keyword
* @return
*/
private String getHitCode(String str , String keyword) {
if(StringUtils.isEmpty(keyword) || StringUtils.isEmpty(str)) {
return str;
}
String lowerCaseStr = str.toLowerCase();
String lowerKeyword = keyword.toLowerCase();
String [] lowerCaseArray = lowerCaseStr.split(lowerKeyword);
Boolean isEndWith = lowerCaseStr.endsWith(lowerKeyword);
// 计算分割后的字符串位置
Integer count = 0;
List<Map<String, Integer>> list = new ArrayList<>();
List<Map<String, Integer>> keyList = new ArrayList<>();
for(int a=0; a<lowerCaseArray.length; a++) {
// 将切割出来的存储map
Map<String, Integer> map = new HashMap<>();
Map<String, Integer> keyMap = new HashMap<>();
map.put("startIndex", count);
Integer len = lowerCaseArray[a].length();
count += len;
map.put("endIndex", count);
list.add(map);
if(a < lowerCaseArray.length -1 || isEndWith) {
// 将keyword存储map
keyMap.put("startIndex", count);
count += keyword.length();
keyMap.put("endIndex", count);
keyList.add(keyMap);
}
}
// 截取切割对象
List<String> arrayList = new ArrayList<>();
for(Map<String, Integer> item : list) {
Integer start = item.get("startIndex");
Integer end = item.get("endIndex");
String itemStr = str.substring(start, end);
arrayList.add(itemStr);
}
// 截取关键字
List<String> keyArrayList = new ArrayList<>();
for(Map<String, Integer> item : keyList) {
Integer start = item.get("startIndex");
Integer end = item.get("endIndex");
String itemStr = str.substring(start, end);
keyArrayList.add(itemStr);
}
String startStr = "<span style = 'color:red'>";
String endStr = "</span>";
StringBuffer sb = new StringBuffer();
for(int a=0; a<arrayList.size(); a++) {
sb.append(arrayList.get(a));
if(a < arrayList.size() - 1 || isEndWith) {
sb.append(startStr);
sb.append(keyArrayList.get(a));
sb.append(endStr);
}
}
return sb.toString();
}
从下图我们能够看到,搜索nginx后,能够把大写开头的Nginx,和小写的nginx都能够显示高亮
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。