现在有字符串"struts StrUts hibernate",我想把 不区分大小写匹配struts的字符串替换为<font color='red'>匹配字符串</font>
近期在做一个搜索引擎,需要匹配字符高亮显示,鄙人经过一番搜索现总结代码如下:
package ;
import ;
import ;
public class XXXX {
/**
* @param args
*/
public static void main(String[] args) {
String source="struts StrUts hibernate";
String patternstring="struts";
(IgnoreCaseReplace(source, patternstring));
}
/**
* java实现不区分大小写高亮替换
* @param source
* @param patternstring
* @return
*/
public static String IgnoreCaseReplace(String source, String patternstring) {
Pattern p = (patternstring, Pattern.CASE_INSENSITIVE);
Matcher mc = (source);
StringBuffer sb = new StringBuffer();
while (()) {
(sb, "<font color='red' >"+()+"</font>");
}
(sb);
return ();
}
}
输出结果如下:
<font color='red' >struts</font> <font color='red' >StrUts</font> hibernate