这是一个场景。
String strText = "ABC abc Abc aBC abC aBc ABc AbC";
// Adding a HTML content to this
String searchText = "abc";
String strFormatted = strText.replaceAll(
"(?i)" + searchText,
"<font color='red'>" + searchText + "</font>");
这将返回一个字符串,其中所有单词都用小写,当然还有红色。我的要求是将strFormatted
作为字符串,大小写与原始字符串相同,但它应该有Font标记。
有可能做到这一点吗?
您可以使用反向引用。类似于:
String strFormatted = strText.replaceAll(
"(?i)(" + searchText + ")",
"<font color='red'>$1</font>");
我想建议使用ArrayList
的替代方案
String [] strText = {"ABC", "abc","Abc", "aBC", "abC", "aBc", "ABc", "AbC"};
ArrayList<String> abc = new ArrayList<String> ();
for(int j=0;j<8;j++)
{
if("abc".equalsIgnoreCase(strText[j]))
{
abc.add("<font color='red'>"+strText[j]+"</font>");
}
}
String strFormatted = abc.toString();
System.out.println(strFormatted);