Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

convernting to camel caps

0 views
Skip to first unread message

Roedy Green

unread,
Jan 2, 2010, 3:37:45 PM1/2/10
to
Java makes great use of camel caps, e.g. words like StringBuilder

Here is how you might automatically convert lower case words to camel
cap form. I use it to generate a first cut at a web site name from a
lower case URL. See the results at
http://mindprod.com/jgloss/minorhassle.html

// converting a lower case word to camel caps.

/**
* capitalise common words embedded in name, and remove dashes,
marking the spots with caps.
*
* @param name name to camel-capitalise
*
* @return name with caps.
*/
private static String toCamel( String name )
{
return toCamelCaps( toCamelDashes( name ) );
}

/**
* remove dashes separating words in name, replacing with Camel
case
*
* @param name to Camel case
*
* @return name with dashes removed.
*/
private static String toCamelDashes( String name )
{
// remove dashes, replacing with caps.
final StringBuilder sb = new StringBuilder( name.length() );
for ( int i = 0; i < name.length(); i++ )
{
char c = name.charAt( i );
if ( c == '-' )
{
// drop the -, append the following char capitalised
final int next = i + 1;
if ( next < name.length() )
{
sb.append( Character.toUpperCase( name.charAt(
next ) ) );
// we processed one more char than usual
i++;
}
}
else
{
sb.append( c );
}
} // end for
return sb.toString();
}

/**
* capitalise embedded words, Camel case style
*
* @param name name to capitalise.
*
* @return name with embedded words capitalised.
*/
private static String toCamelCaps( String name )
{
// capitalise any embedded words
final StringBuilder sb = new StringBuilder( name.length() );
boolean capitaliseNextLetter = true;
outer:
for ( int i = 0; i < name.length(); i++ )
{
// see if any toCamel words start at this position in name
inner:
for ( String word : camelWords )
{
if ( name.substring( i ).startsWith( word ) )
{
// capitalise this word and the next.
sb.append( Character.toUpperCase( name.charAt( i )
) );
sb.append( word.substring( 1 ) );
i += word.length() - 1;
capitaliseNextLetter = true;
continue outer;
}
}
// end inner loop without finding any matches.

if ( capitaliseNextLetter )
{
sb.append( Character.toUpperCase( name.charAt( i ) )
);
capitaliseNextLetter = false;
}
else
{
sb.append( name.charAt( i ) );
}
}
// end outer loop
return sb.toString();
}


/**
* list of words often used in domain names. All must be lower
case.
*/
private static String[] camelWords =
{
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"about",
"active",
"add",
"all",
"allied",
"alpha",
"alternative",
"and",
"arcade",
"arch",
"bank",
"best",
"big",
"bigger",
"blue",
"box",
"bronze",
"bulletin",
"buy",
"cellular",
"chief",
"completely",
"content",
"cool",
"cope",
"corner",
"data",
"deep",
"depot",
"designer",
"dir",
"direct",
"directory",
"discovery",
"doc",
"dog",
"donkey",
"download",
"downloads",
"dump",
"easy",
"exchange",
"faq",
"field",
"fields",
"file",
"find",
"for",
"forum",
"free",
"frog",
"fusion",
"game",
"games",
"garage",
"germany",
"get",
"go",
"gold",
"green",
"guide",
"have",
"horse",
"host",
"hosts",
"hot",
"how",
"hub",
"ice",
"info",
"information",
"internet",
"jet",
"key",
"king",
"knight",
"knowledge",
"landmark",
"library",
"link",
"links",
"list",
"load",
"machine",
"master",
"media",
"mega",
"million",
"mini",
"mobile",
"money",
"monster",
"move",
"mp3",
"must",
"my",
"nano",
"net",
"news",
"newz",
"now",
"note",
"o2p",
"office",
"one",
"online",
"pad",
"page",
"paradise",
"pedia",
"phone",
"pick",
"pile",
"platinum",
"popular",
"press",
"prime",
"project",
"purely",
"quality",
"rank",
"red",
"research",
"resource",
"rocket",
"search",
"seek",
"shareware",
"simple",
"site",
"slash",
"smooth",
"soft",
"software",
"source",
"spaces",
"spider",
"standard",
"stop",
"store",
"storm",
"submit",
"suggest",
"super",
"tail",
"tech",
"the",
"titanium",
"transform",
"trial",
"url",
"video",
"videos",
"viz",
"ware",
"water",
"web",
"which",
"wide",
"wiki",
"windows",
"wing",
"wire",
"wise",
"working",
"world",
"write",
"zone",
};


static
{
// put longest strings first
Arrays.sort( camelWords, new LongestFirst() );
}

--
Roedy Green Canadian Mind Products
http://mindprod.com
If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.

0 new messages