Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

StringBuilder#insertCodePoint

Thread view: 
Stefan Ram - 09 May 2006 18:13 GMT
 I have a code point »cp«, which I would like to insert
 into a StringBuilder-object »sb« at position 0. So:

sb.insertCodePoint( 0, cp );

 The problem is that there is no such method, and I
 wonder what the most simple / most straigthforward
 way is. I can't even seem to find the constructor

new java.lang.String( cp )

 to build a string containing only that code point.

 Am I really supposed to write:

sb.insert( 0, new java.lang.String( new int[]{ cp }, 0, 1 ));
Chris Uppal - 09 May 2006 19:09 GMT
>   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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.