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
posted
0
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
posted
0
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
posted
0
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: 35241
7
posted
0
What is the best way to check whether a string is uppercase?
For this part the Simplest might be "if (str.equals(str.toUpperCase())) ..."
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
posted
0
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:
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...