开发者

String transforming with danger symbols

I have the followings string:

String l = "1. [](a+b)\n2. (-(q)*<>(r))\n3. 00(d)\n4. (a+-b)";
String s = "1. [](a+b)\n2. 00开发者_JAVA技巧(d)"

First string is a expresions list. Sencond string is a expresions subset of first string, but theirs number-ids aren't equals.

Then, I want to do this:

String l2 = "1. <b>[](a+b)</b>\n2. (-(q)*<>(r))\n3. <b>00(d)</b>\n4. (a+-b)";

l2 is a transformation of l but with expressions marked. This marked expresions are contains in s. Note that originals strings have symbols as (, [, and so on What is better way to do it?


If I understand you correctly, your problem ist that

String.replaceAll(String, String)

doesn't work here, because the Characters (, [, \, have special meanings in regular expressions.

Maybe you could just use

String.replace(CharSequence target, CharSequence replacement)

Then you don't need to deal with regular expressions at all.


    String l = "1. [](a+b)\n2. (-(q)*<>(r))\n3. 00(d)\n4. (a+-b)";
    String s = "1. [](a+b)\n2. 00(d)";

    String[] a=l.split("\\n");
    for(int i=0;i<a.length;i++)
        a[i]=a[i].split("\\d. ")[1];
    String[] b=s.split("\\n");
    for(int i=0;i<b.length;i++)
        b[i]=b[i].split("\\d. ")[1];

    for(int i=0;i<b.length;i++)
      l=l.replace(b[i], "<b>"+b[i]+"</b>");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜