2

这是场景。

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字符串,其大小写与原始字符串相同,但它应该具有字体标签。

是否有可能做到这一点 ?

4

2 回答 2

6

您可以使用反向引用。就像是:

String strFormatted = strText.replaceAll(
    "(?i)(" + searchText + ")", 
    "<font color='red'>$1</font>");
于 2012-01-06T04:07:33.923 回答
0

我想建议使用替代方法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);
于 2012-01-06T05:05:15.687 回答