This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes best way to check whether a character is uppercase? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "best way to check whether a character is uppercase?" Watch "best way to check whether a character is uppercase?" New topic
Author

best way to check whether a character is uppercase?

Sean Gildea
Ranch Hand

Joined: Jul 01, 2004
Posts: 81
What is the best way to check whether a string is uppercase?

I need to convert this string "thisMethodNeedsToBeParsed"
into "this method needs to be parsed"

can anyone think of the fastest / best way to do it?

thanks in advance!


SCJD, SCBCD, SCJP
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Offhand, I can't think of anything clever. But you can always go char by char...

[ August 08, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Kuldeep Vaishnav
Ranch Hand

Joined: May 23, 2004
Posts: 72
another way is to see if the character is upper case by comparing its ascii value.

Top of my head some rough algo..............
--------------------------------------------
for(var i=0;i till length of string;i++)
var char = charOfString(i);
var asciiVal = ASCII(char);
var resultinString;
if(asciiVal between 65 to 90)
add "" and char to resultinString
else
add char to resultingString
for end
---------------------------------------------


Kuldeep
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
if(asciiVal between 65 to 90)


Of course, this fails when using non-ascii character sets. There is a way that uses the same concept. Given that most character sets have the upper case letters consecutively, you can just ask

Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Timmy Marks:
Of course, this fails when using non-ascii character sets.


True.

There is a way that uses the same concept. Given that most character sets have the upper case letters consecutively, you can just ask


Well, of course we know that Java always uses unicode, which has a lot more uppercase characters then just A-Z. So in Java, your solution fails, too.

Anyway, why reinvent the wheel if Sun already did all the work for you?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41025
    
  43

What is the best way to check whether a string is uppercase?


For this part the Simplest might be "if (str.equals(str.toUpperCase())) ..."


Ping & DNS - my free Android networking tools app
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262

We can't know what the best way is without a lot more context, but this way seems to require the least typing.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112

We can't know what the best way is without a lot more context, but this way seems to require the least typing.[/qb]<hr></blockquote>

Or even better (because it also applies to uppercase letters outside the A-Z block):



(I'm not sure that "$0" actually works - JavaDoc only seems to mention "\0"...)

[sorry for the german that somehow slipped in...]
[ August 10, 2005: Message edited by: Ilja Preuss ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Alan Moore:
... " $0" ...

Very nice! That's the "clever" approach I suspected was possible, but I'm not up enough on regex to come up with that myself.

I've been looking at the Pattern class API, but I can't figure out how this works. How does $0 (or \\0) translate to the matched character?
[ August 09, 2005: Message edited by: marc weber ]
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
When used in the replacement string, "$0" is replaced by whatever was matched by the regex. In this case, the regex only matches one character, so that's what gets inserted. This mechanism is described in the javadoc for the appendReplacement() method of the Matcher class:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Matcher.html
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Thanks, Alan!

I didn't realize that String's replaceAll(String regex, String replacement) method could "pass" information from the regex matcher to the replacement String. Very interesting...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: best way to check whether a character is uppercase?
 
Similar Threads
String objects
Lost, need help
Check for the given String is a integer
String - can you improve my poor code?
help with Jboss datasource lookup with springs