> I have a code point »cp«, which I would like to insert
> into a StringBuilder-object »sb« at position 0. So:
I'm assuming that since you mention code points, then you do mean Unicode
rather than 16-bit char values. I'm also assuming that you don't fancy messing
around with UTF-16 encoding yourself.
> sb.insert( 0, new java.lang.String( new int[]{ cp }, 0, 1 ));
You can improve on that a /bit/:
sb.insert(0, new StringBuilder().appendCodePoint(cp));
I leave it to you to choose which you find least unattractive...
-- chris
Stefan Ram - 09 May 2006 19:44 GMT
>>sb.insert( 0, new java.lang.String( new int[]{ cp }, 0, 1 ));
>sb.insert(0, new StringBuilder().appendCodePoint(cp));
I will use one of those and just believe (in the religious
sense) that the Hotspot JIT-Compiler will transform this to
something as efficient as
sb.insertCodePoint( cp )
Chris Uppal - 10 May 2006 09:27 GMT
> I will use one of those and just believe (in the religious
> sense)
;-)
> that the Hotspot JIT-Compiler will transform this to
> something as efficient as
>
> sb.insertCodePoint( cp )
Since inserting into a string is an O(n) operation anyway, I doubt it if makes
much difference.
(But you knew that already, of course)
-- chris