Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Gelfin

macrumors 68020
Sep 18, 2001
2,165
5
Denver, CO
Actually, substring() is a bit more intelligent than that. All the Strings created using substring() will use the same underlying character array (since Strings are immutable in Java). A new String container will still be created for each substring, which still makes the loop more efficient.

Ooo, darn meddling kids. You got me. :)
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Look again. You created lots of Character objects, but I didn't create any. The character utility functions are static for exactly that reason. Character.isLetter(x) is the way to test. Even if you didn't have that, you can still use comparison operators on primitive types:

Code:
if((x >= 'A' && x <= 'Z') || (x >='a' && x <= 'z')) {...}

<homer>Doh!</homer> Sorry! Spent too long at work looking, mostly, at Java code! I don't personally like the primitive comparisons. The assumption there is that we are using the "standard" 26 character alphabet. I'm pretty sure that would not count é as a letter, but isLetter() would.
 

Zortrium

macrumors 6502
Jun 23, 2003
461
0
Slight efficiency losses aside, recursion still strikes me as the most elegant and readable solution-then again, my current recursion tendencies may largely be that I've been doing Lisp recently. ;)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.