开发者

How to wrap "<pre>" tag around whitespaces in a java String?

Suppose I have a long java String , I need to wrap all whitespaces (lengt开发者_StackOverflowh >=2) with <pre></pre> tag... , How to achieve that ?

ex :

before :
String str = "<font color=\"#000000\">A B  C   D</font>"

after :
<font color=\"#000000\">A B<pre>  </pre>C<pre>   </pre>D</font>

Between A and B is one whitespace , and not wrapped.
Between B and C is two whitespaces , and it is wrapped.
Between C and D is three whitespaces , and it is wrapped,too.

How to achieve that in RegExp ? Thanks a lot !


How about a CSS solution using white-space:

white-space: pre

With this sequences of white space are not collapsed. So:

<font color="#000000" style="white-space:pre">A B  C   D</font>


That will probably not do what you want... Will a &nbsp; do the job you want? Also, I'd seriously consider replacing spacing with <span style="width: 10px; display: inline-block;"> </span>. Or even a proper table...


why don't you simply set white-space:pre-wrap or white-space:pre as css for the element containing your string? i think this is what you're trying to archive.

(or, even more easy, use &nbsp; instead of ' ' (normal space))


    String s = "123 123  123";
    s = s.replaceAll("(  )+", "<pre>$1</pre>");

The String s then contains 123 123<pre> </pre>123

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜