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、元の文字列と同じ大文字と小文字の文字列として取得することですが、Fontタグが必要です。

これを行うことは可能ですか?

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 に答える