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 / September 2007

Tip: Looking for answers? Try searching our database.

how to convert like aa_bb_cc to AaBbCc efficiency in java

Thread view: 
sss.zhou@gmail.com - 14 Sep 2007 16:28 GMT
I want write an function to convert the abc_def_gh to AbcDefGh.

What I thought was, but I think it is very unefficient

>>> code here:
String[] nameWords = name.split("_");

StringBuffer className = new StringBuffer();

for (int i=0; i<nameWords.length; i++) {

    if (nameWords[i].length() > 0) {
        char c = nameWords[i].charAt(0);
        className.append(Character.toUpperCase(c));
        className.append(nameWords[i].substring(1));
    }
}

then the className.toString() is what I need.

And can you tell me some efficient way of doing this?

I tried to use the regular expression.
name.replaceAll("[a-z]{1}_", "what here?"), but I can't write the
second arguments.
Thomas Fritsch - 14 Sep 2007 17:41 GMT
> I want write an function to convert the abc_def_gh to AbcDefGh.
>
[quoted text clipped - 18 lines]
> name.replaceAll("[a-z]{1}_", "what here?"), but I can't write the
> second arguments.

I guess there are 2 hot-spots consuming much time:
(1) the split() call, because regular expressions are a *heavy* thing
(2) the substring() calls, because a new String object is created each time

They can be avoided with little effort:

 StringBuffer className = new StringBuffer();
 boolean needUpperCase = true;
 for (int i = 0; i < s.length(); i++) {
   char c = s.charAt(i);
   if (c == '_') {
     needUpperCase = true;
     c = s.charAt(++i);
   }
   className.append(needUpperCase ? Character.toUpperCase(c) : c);
   needUpperCase = false;
 }

(My use of ?/: instead if/else doesn't make it more efficient, but it makes
the code 3 lines shorter.)

Signature

Thomas

Roedy Green - 19 Sep 2007 17:43 GMT
On Fri, 14 Sep 2007 16:41:06 GMT, Thomas Fritsch
<i.dont.like.spam@invalid.com> wrote, quoted or indirectly quoted
someone who said :

>  className.append(needUpperCase ? Character.toUpperCase(c) : c);
>    needUpperCase = false;

You might go for efficient code just for a learning exercise, but it
the real world, you would leave this method be unless it proved to be
a bottleneck.

toUpperCase is a hairy routine with all manner of coded cultural lore
on how caps are done for various symbol sets.  If you are just dealing
with English, you can write a much simpler version that goes like
this:

static char toUpperCase( char c )
  {
 return  'a' <= c && c <= 'z'  ? (char)( c  + ('A'-'a')) : c;
  }
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 19 Sep 2007 20:02 GMT
On Wed, 19 Sep 2007 16:43:48 GMT, Roedy Green
<see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
someone who said :

>toUpperCase is a hairy routine with all manner of coded cultural lore
>on how caps are done for various symbol sets.  If you are just dealing
[quoted text clipped - 5 lines]
>  return  'a' <= c && c <= 'z'  ? (char)( c  + ('A'-'a')) : c;
>   }

The code for all four methods is part of StringTools.  See
http://mindprod.com/products1.html#COMMON11

   /**
    * Quick replacement for Character.toLowerCase for use with
English-only. It
    * does not deal with accented characters.
    *
    * @param c character to convert
    *
    * @return character converted to lower case
    */
   static char toLowerCase( char c )
       {
       return 'A' <= c && c <= 'Z' ? (char) ( c + ( 'a' - 'A' ) ) :
c;
       }

   /**
    * Quick replacement for Character.toLowerCase for use with
English-only. It
    * does not deal with accented characters.
    *
    * @param s String to convert
    *
    * @return String converted to lower case
    */
   static String toLowerCase( String s )
       {
       final char[] ca = s.toCharArray();
       final int length = ca.length;
       boolean changed = false;
       // can't use for:each since we need the index to set.
       for ( int i = 0; i < length; i++ )
           {
           final char c = ca[ i ];
           if ( 'A' <= c && c <= 'Z' )
               {
               // found a char that needs conversion.
               ca[ i ] = (char) ( c + ( 'a' - 'A' ) );
               changed = true;
               }
           }
       // give back same string if unchanged.
       return changed ? new String( ca ) : s;
       }

   /**
    * Quick replacement for Character.toUpperCase for use with
English-only. It
    * does not deal with accented characters.
    *
    * @param c character to convert
    *
    * @return character converted to upper case
    */
   static char toUpperCase( char c )
       {
       return 'a' <= c && c <= 'z' ? (char) ( c + ( 'A' - 'a' ) ) :
c;
       }

   /**
    * Quick replacement for Character.toUpperCase for use with
English-only. It
    * does not deal with accented characters.
    *
    * @param s String to convert
    *
    * @return String converted to upper case
    */
   static String toUpperCase( String s )
       {
       final char[] ca = s.toCharArray();
       final int length = ca.length;
       boolean changed = false;
       // can't use for:each since we need the index to set.
       for ( int i = 0; i < length; i++ )
           {
           final char c = ca[ i ];
           if ( 'a' <= c && c <= 'z' )
               {
               // found a char that needs conversion.
               ca[ i ] = (char) ( c + ( 'A' - 'a' ) );
               changed = true;
               }
           }
       // give back same string if unchanged.
       return changed ? new String( ca ) : s;
       }

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



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



©2008 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.