how to convert like aa_bb_cc to AaBbCc efficiency in java

S

sss.zhou

I want write an function to convert the abc_def_gh to AbcDefGh.

What I thought was, but I think it is very unefficient
String[] nameWords = name.split("_");

StringBuffer className = new StringBuffer();

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

if (nameWords.length() > 0) {
char c = nameWords.charAt(0);
className.append(Character.toUpperCase(c));
className.append(nameWords.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.
 
T

Thomas Fritsch

I want write an function to convert the abc_def_gh to AbcDefGh.

What I thought was, but I think it is very unefficient
String[] nameWords = name.split("_");
StringBuffer className = new StringBuffer();
for (int i=0; i<nameWords.length; i++) {
if (nameWords.length() > 0) {
char c = nameWords.charAt(0);
className.append(Character.toUpperCase(c));
className.append(nameWords.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.


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.)
 
R

Roedy Green

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;
}
 
R

Roedy Green

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;
}

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top