Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

best way to check whether a character is uppercase?

 
Sean Gildea
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Offhand, I can't think of anything clever. But you can always go char by char...

[ August 08, 2005: Message edited by: marc weber ]
 
Kuldeep Vaishnav
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
---------------------------------------------
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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


For this part the Simplest might be "if (str.equals(str.toUpperCase())) ..."
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic